Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the circular color picker from Android SDK samples. Edited samp…

…le Activity.
  • Loading branch information...
commit e8c176cee02edeee88bd9e5bf6175bd105ba46e5 1 parent de4f4da
@Cheesebaron authored
View
5 .gitignore
@@ -3,4 +3,7 @@ bin
obj
# mstest test results
-TestResults
+TestResults
+
+# ReSharper
+_ReSharper.*
View
BIN  MonoDroid.ColorPickers.suo
Binary file not shown
View
61 MonoDroid.ColorPickers/Activity1.cs
@@ -10,32 +10,59 @@ namespace MonoDroid.ColorPickers
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
+ private Button _btNoAlpha;
+ private Button _btAlpha;
+ private Button _btRound;
+ private ColorPickerPanelView _panelNoAlpha;
+ private ColorPickerPanelView _panelAlpha;
+ private ColorPickerPanelView _panelRound;
+
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
- var colorPicker = new ColorPickerDialog(this, Color.Azure);
- colorPicker.ColorChanged += (sender, args) => Console.WriteLine("Color changed: {0}", args.Color);
+ SetContentView(Resource.Layout.Main);
+
+ _btNoAlpha = FindViewById<Button>(Resource.Id.ButtonColorNoAlpha);
+ _btAlpha = FindViewById<Button>(Resource.Id.ButtonColorAlpha);
+ _btRound = FindViewById<Button>(Resource.Id.ButtonRoundColor);
- var colorPickerAlpha = new ColorPickerDialog(this, Color.Aqua);
- colorPickerAlpha.ColorChanged += (sender, args) => Console.WriteLine("Color changed: {0}", args.Color);
- colorPickerAlpha.AlphaSliderVisible = true;
+ _btNoAlpha.Click += BtNoAlphaOnClick;
+ _btAlpha.Click += BtAlphaOnClick;
+ _btRound.Click += BtRoundOnClick;
- var ll = new LinearLayout(this)
- {
- LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FillParent,
- ViewGroup.LayoutParams.FillParent)
- };
+ _panelNoAlpha = FindViewById<ColorPickerPanelView>(Resource.Id.PanelColorNoAlpha);
+ _panelNoAlpha.Color = Color.Black;
+ _panelAlpha = FindViewById<ColorPickerPanelView>(Resource.Id.PanelColorAlpha);
+ _panelAlpha.Color = Color.Black;
+ _panelRound = FindViewById<ColorPickerPanelView>(Resource.Id.PanelRoundColor);
+ _panelRound.Color = Color.Black;
+ }
- var btColorPicker = new Button(this) { Text = "Show Color Picker" };
- btColorPicker.Click += (sender, args) => colorPicker.Show();
+ private void BtRoundOnClick(object sender, EventArgs eventArgs)
+ {
+ var roundColorPickerDialog = new RoundColorPickerDialog(this, _panelRound.Color);
+ roundColorPickerDialog.ColorChanged += (o, args) => _panelRound.Color = args.Color;
+ roundColorPickerDialog.Show();
+ }
- var btColorPickerAlpha = new Button(this) { Text = "Show Color Picker (Alpha)" };
- btColorPickerAlpha.Click += (sender, args) => colorPickerAlpha.Show();
+ private void BtAlphaOnClick(object sender, EventArgs eventArgs)
+ {
+ using (var colorPickerDialog = new ColorPickerDialog(this, _panelAlpha.Color))
+ {
+ colorPickerDialog.AlphaSliderVisible = true;
+ colorPickerDialog.ColorChanged += (o, args) => _panelAlpha.Color = args.Color;
+ colorPickerDialog.Show();
+ }
+ }
- ll.AddView(btColorPicker);
- ll.AddView(btColorPickerAlpha);
- SetContentView(ll);
+ private void BtNoAlphaOnClick(object sender, EventArgs eventArgs)
+ {
+ using (var colorPickerDialog = new ColorPickerDialog(this, _panelNoAlpha.Color))
+ {
+ colorPickerDialog.ColorChanged += (o, args) => _panelNoAlpha.Color = args.Color;
+ colorPickerDialog.Show();
+ }
}
}
}
View
12 MonoDroid.ColorPickers/ColorChangedEventArgs.cs
@@ -0,0 +1,12 @@
+using System;
+using Android.Graphics;
+
+namespace MonoDroid.ColorPickers
+{
+ public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);
+
+ public class ColorChangedEventArgs : EventArgs
+ {
+ public Color Color { get; set; }
+ }
+}
View
6 MonoDroid.ColorPickers/ColorPickerDialog.cs
@@ -94,7 +94,7 @@ private void SetUp(Color color)
{
_newColor.Color = args.Color;
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = _newColor.Color });
+ ColorChanged(this, new ColorChangedEventArgs { Color = _newColor.Color });
};
_oldColor.Color = color;
_colorPicker.Color = color;
@@ -106,11 +106,11 @@ public void OnClick(View v)
{
case Resource.Id.new_color_panel:
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = _newColor.Color });
+ ColorChanged(this, new ColorChangedEventArgs { Color = _newColor.Color });
break;
case Resource.Id.old_color_panel:
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = _oldColor.Color });
+ ColorChanged(this, new ColorChangedEventArgs { Color = _oldColor.Color });
break;
}
Dismiss();
View
4 MonoDroid.ColorPickers/ColorPickerPanelView.cs
@@ -27,7 +27,7 @@
namespace MonoDroid.ColorPickers
{
- public delegate void PanelClickedEventHandler(object sender, ColorEventArgs e);
+ public delegate void PanelClickedEventHandler(object sender, ColorChangedEventArgs e);
class ColorPickerPanelView: View
{
@@ -165,7 +165,7 @@ public override bool OnTouchEvent(MotionEvent e)
if (e.Action == MotionEventActions.Up || e.Action == MotionEventActions.Down)
{
if (PanelClicked != null)
- PanelClicked(this, new ColorEventArgs{ Color = _color });
+ PanelClicked(this, new ColorChangedEventArgs { Color = _color });
}
return base.OnTouchEvent(e);
View
107 MonoDroid.ColorPickers/ColorPickerView.cs
@@ -27,13 +27,6 @@
namespace MonoDroid.ColorPickers
{
- public delegate void ColorChangedEventHandler(object sender, ColorEventArgs e);
-
- public class ColorEventArgs : EventArgs
- {
- public Color Color { get; set; }
- }
-
public class ColorPickerView : View
{
#region Fields
@@ -160,7 +153,7 @@ public Color Color
_val = hsv.V;
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = ColorUtils.ColorFromHSV(_hue / 360, _sat, _val, _alpha) });
+ ColorChanged(this, new ColorChangedEventArgs { Color = ColorUtils.ColorFromHSV(_hue / 360, _sat, _val, _alpha) });
Invalidate();
}
@@ -326,13 +319,14 @@ protected override void OnDraw(Canvas canvas)
var rgb = ColorUtils.ColorFromHSV(_hue/360f,1f,1f);
- _satShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
- Color.Argb(255,255,255,255), rgb, Shader.TileMode.Clamp);
- var mShader = new ComposeShader(_valShader, _satShader, PorterDuff.Mode.Multiply);
- _satValPaint.SetShader(mShader);
-
- canvas.DrawRect(rect, _satValPaint);
+ using (_satShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
+ Color.Argb(255,255,255,255), rgb, Shader.TileMode.Clamp))
+ {
+ var mShader = new ComposeShader(_valShader, _satShader, PorterDuff.Mode.Multiply);
+ _satValPaint.SetShader(mShader);
+ canvas.DrawRect(rect, _satValPaint);
+ }
var p = SatValToPoint(_sat, _val);
_satValTrackerPaint.Color = Color.Argb(255,0,0,0);
@@ -432,13 +426,13 @@ private Point HueToPoint(float hue)
var color = ColorUtils.ColorFromHSV(_hue/360f,_sat,_val);
var acolor = ColorUtils.ColorFromHSV(_hue/360f, _sat, _val, 0);
- _alphaShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
- color, acolor, Shader.TileMode.Clamp);
-
-
- _alphaPaint.SetShader(_alphaShader);
+ using (_alphaShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
+ color, acolor, Shader.TileMode.Clamp))
+ {
+ _alphaPaint.SetShader(_alphaShader);
- canvas.DrawRect(rect, _alphaPaint);
+ canvas.DrawRect(rect, _alphaPaint);
+ }
if(!string.IsNullOrEmpty(_alphaSliderText)){
canvas.DrawText(_alphaSliderText, rect.CenterX(), rect.CenterY() + 4 * _density, _alphaTextPaint);
@@ -610,7 +604,7 @@ public override bool OnTrackballEvent(MotionEvent e)
if(update)
{
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = ColorUtils.ColorFromHSV(_hue/360, _sat, _val, _alpha)});
+ ColorChanged(this, new ColorChangedEventArgs { Color = ColorUtils.ColorFromHSV(_hue / 360, _sat, _val, _alpha) });
Invalidate();
return true;
@@ -641,7 +635,7 @@ public override bool OnTouchEvent(MotionEvent e)
if(update)
{
if (ColorChanged != null)
- ColorChanged(this, new ColorEventArgs { Color = ColorUtils.ColorFromHSV(_hue/360, _sat, _val, _alpha)});
+ ColorChanged(this, new ColorChangedEventArgs { Color = ColorUtils.ColorFromHSV(_hue / 360, _sat, _val, _alpha) });
Invalidate();
return true;
@@ -840,5 +834,74 @@ private int GetPrefferedHeight()
return height;
}
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ if (null != _satValPaint)
+ {
+ _satValPaint.Dispose();
+ _satValPaint = null;
+ }
+ if (null != _satValTrackerPaint)
+ {
+ _satValTrackerPaint.Dispose();
+ _satValTrackerPaint = null;
+ }
+ if (null != _huePaint)
+ {
+ _huePaint.Dispose();
+ _huePaint = null;
+ }
+ if (null != _hueTrackerPaint)
+ {
+ _hueTrackerPaint.Dispose();
+ _hueTrackerPaint = null;
+ }
+ if (null != _alphaPaint)
+ {
+ _alphaPaint.Dispose();
+ _alphaPaint = null;
+ }
+ if (null != _alphaTextPaint)
+ {
+ _alphaTextPaint.Dispose();
+ _alphaTextPaint = null;
+ }
+ if (null != _hueShader)
+ {
+ _hueShader.Dispose();
+ _hueShader = null;
+ }
+ if (null != _borderPaint)
+ {
+ _borderPaint.Dispose();
+ _borderPaint = null;
+ }
+ if (null != _valShader)
+ {
+ _valShader.Dispose();
+ _valShader = null;
+ }
+ if (null != _satShader)
+ {
+ _satShader.Dispose();
+ _satShader = null;
+ }
+ if (null != _alphaShader)
+ {
+ _alphaShader.Dispose();
+ _alphaShader = null;
+ }
+ if (null != _alphaPattern)
+ {
+ _alphaPattern.Dispose();
+ _alphaPattern = null;
+ }
+ }
+
+ base.Dispose(disposing);
+ }
}
}
View
3  MonoDroid.ColorPickers/MonoDroid.ColorPickers.csproj
@@ -48,12 +48,15 @@
<ItemGroup>
<Compile Include="Activity1.cs" />
<Compile Include="AlphaPatternDrawable.cs" />
+ <Compile Include="ColorChangedEventArgs.cs" />
<Compile Include="ColorPickerDialog.cs" />
<Compile Include="ColorPickerPanelView.cs" />
<Compile Include="ColorPickerView.cs" />
<Compile Include="ColorUtils.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RoundColorPickerDialog.cs" />
+ <Compile Include="RoundColorPickerView.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
View
40 MonoDroid.ColorPickers/Resources/Layout/Main.axml
@@ -1,7 +1,33 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
-</LinearLayout>
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent">
+ <Button
+ android:text="@string/button_color_picker_noalpha"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/ButtonColorNoAlpha" />
+ <monodroid.colorpickers.ColorPickerPanelView
+ android:id="@+id/PanelColorNoAlpha"
+ android:layout_width="fill_parent"
+ android:layout_height="50dp" />
+ <Button
+ android:text="@string/button_color_picker_alpha"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/ButtonColorAlpha" />
+ <monodroid.colorpickers.ColorPickerPanelView
+ android:id="@+id/PanelColorAlpha"
+ android:layout_width="fill_parent"
+ android:layout_height="50dp" />
+ <Button
+ android:text="@string/button_color_picker_round"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/ButtonRoundColor" />
+ <monodroid.colorpickers.ColorPickerPanelView
+ android:id="@+id/PanelRoundColor"
+ android:layout_width="fill_parent"
+ android:layout_height="50dp" />
+</LinearLayout>
View
48 MonoDroid.ColorPickers/Resources/Resource.Designer.cs
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
-// Runtime Version:4.0.30319.269
+// Runtime Version:4.0.30319.17626
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -37,6 +37,24 @@ private Drawable()
public partial class Id
{
+ // aapt resource value: 0x7f050005
+ public const int ButtonColorAlpha = 2131034117;
+
+ // aapt resource value: 0x7f050003
+ public const int ButtonColorNoAlpha = 2131034115;
+
+ // aapt resource value: 0x7f050007
+ public const int ButtonRoundColor = 2131034119;
+
+ // aapt resource value: 0x7f050006
+ public const int PanelColorAlpha = 2131034118;
+
+ // aapt resource value: 0x7f050004
+ public const int PanelColorNoAlpha = 2131034116;
+
+ // aapt resource value: 0x7f050008
+ public const int PanelRoundColor = 2131034120;
+
// aapt resource value: 0x7f050000
public const int color_picker_view = 2131034112;
@@ -71,36 +89,18 @@ public partial class String
// aapt resource value: 0x7f040000
public const int app_name = 2130968576;
- // aapt resource value: 0x7f040005
- public const int color1_summary = 2130968581;
-
// aapt resource value: 0x7f040004
- public const int color1_title = 2130968580;
+ public const int button_color_picker_alpha = 2130968580;
- // aapt resource value: 0x7f040007
- public const int color2_summary = 2130968583;
-
- // aapt resource value: 0x7f040006
- public const int color2_title = 2130968582;
-
- // aapt resource value: 0x7f040009
- public const int color3_summary = 2130968585;
-
- // aapt resource value: 0x7f040008
- public const int color3_title = 2130968584;
-
- // aapt resource value: 0x7f04000b
- public const int color4_summary = 2130968587;
+ // aapt resource value: 0x7f040003
+ public const int button_color_picker_noalpha = 2130968579;
- // aapt resource value: 0x7f04000a
- public const int color4_title = 2130968586;
+ // aapt resource value: 0x7f040005
+ public const int button_color_picker_round = 2130968581;
// aapt resource value: 0x7f040001
public const int dialog_color_picker = 2130968577;
- // aapt resource value: 0x7f040003
- public const int pref_category = 2130968579;
-
// aapt resource value: 0x7f040002
public const int press_color_to_apply = 2130968578;
View
17 MonoDroid.ColorPickers/Resources/Values/Strings.xml
@@ -6,15 +6,10 @@
<!-- Color Picker -->
<string name="dialog_color_picker">Color Picker</string>
<string name="press_color_to_apply">Press on Color to apply</string>
-
- <!-- Preferences -->
- <string name="pref_category">Category</string>
- <string name="color1_title">Color 1</string>
- <string name="color1_summary">black color by default, set by reference</string>
- <string name="color2_title">Color 2</string>
- <string name="color2_summary">not persistent color\nalpha slider added via code</string>
- <string name="color3_title">Color 3</string>
- <string name="color3_summary">picker with alpha slider</string>
- <string name="color4_title">Color 4</string>
- <string name="color4_summary">color set with HEX code in xml</string>
+
+ <!-- Buttons -->
+ <string name="button_color_picker_noalpha">Pick Color (Advanced)</string>
+ <string name="button_color_picker_alpha">Pick Color with Alpha (Advanced)</string>
+ <string name="button_color_picker_round">Color Picker (Simple)</string>
+
</resources>
View
61 MonoDroid.ColorPickers/RoundColorPickerDialog.cs
@@ -0,0 +1,61 @@
+/*
+ * Derivative work of ColorPickerDialog.java from Android SDK API samples,
+ * ported to Mono for Android and added .NET style event handling.
+ *
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using Android.App;
+using Android.Content;
+using Android.Graphics;
+using Android.OS;
+using Android.Runtime;
+
+namespace MonoDroid.ColorPickers
+{
+ public class RoundColorPickerDialog : Dialog
+ {
+ public event ColorChangedEventHandler ColorChanged;
+ private static Color _initialColor;
+
+ public RoundColorPickerDialog(Context context, Color initialColor)
+ : base(context)
+ {
+ _initialColor = initialColor;
+ }
+
+ protected RoundColorPickerDialog(IntPtr javaReference, JniHandleOwnership transfer)
+ : base(javaReference, transfer)
+ {
+ _initialColor = Color.Black;
+ }
+
+ protected override void OnCreate(Bundle savedInstanceState)
+ {
+ base.OnCreate(savedInstanceState);
+ var cPickerView = new RoundColorPickerView(Context, _initialColor);
+ cPickerView.ColorChanged += delegate(object sender, ColorChangedEventArgs args)
+ {
+ if (ColorChanged != null)
+ ColorChanged(this, args);
+
+ Dismiss();
+ };
+ SetContentView(cPickerView);
+ SetTitle("Pick a Color");
+ }
+ }
+}
View
198 MonoDroid.ColorPickers/RoundColorPickerView.cs
@@ -0,0 +1,198 @@
+/*
+ * Derivative work of ColorPickerDialog.java from Android SDK API samples,
+ * ported to Mono for Android and added .NET style event handling.
+ *
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using Android.Content;
+using Android.Graphics;
+using Android.Views;
+
+namespace MonoDroid.ColorPickers
+{
+ public class RoundColorPickerView : View
+ {
+ public event ColorChangedEventHandler ColorChanged;
+ private readonly Paint _paint;
+ private readonly Paint _centerPaint;
+ private readonly int[] _colors;
+
+ public RoundColorPickerView(Context c, Color color)
+ : base(c)
+ {
+ _colors = new[] {
+ new Color(255,0,0,255).ToArgb(), new Color(255,0,255,255).ToArgb(), new Color(0,0,255,255).ToArgb(),
+ new Color(0,255,255,255).ToArgb(), new Color(0,255,0,255).ToArgb(), new Color(255,255,0,255).ToArgb(),
+ new Color(255,0,0,255).ToArgb()
+ };
+ Shader s = new SweepGradient(0, 0, _colors, null);
+
+ _paint = new Paint(PaintFlags.AntiAlias);
+ _paint.SetShader(s);
+ _paint.SetStyle(Paint.Style.Stroke);
+ _paint.StrokeWidth = 32;
+
+ _centerPaint = new Paint(PaintFlags.AntiAlias)
+ {
+ Color = color,
+ StrokeWidth = 5
+ };
+ }
+
+ private bool _trackingCenter;
+ private bool _highlightCenter;
+
+ protected override void OnDraw(Canvas canvas)
+ {
+ var r = CenterX - _paint.StrokeWidth * 0.5f;
+
+ canvas.Translate(CenterX, CenterX);
+
+ canvas.DrawOval(new RectF(-r, -r, r, r), _paint);
+ canvas.DrawCircle(0, 0, CenterRadius, _centerPaint);
+
+ if (_trackingCenter)
+ {
+ var c = _centerPaint.Color;
+ _centerPaint.SetStyle(Paint.Style.Stroke);
+
+ _centerPaint.Alpha = _highlightCenter ? 255 : 128;
+
+ canvas.DrawCircle(0, 0,
+ CenterRadius + _centerPaint.StrokeWidth,
+ _centerPaint);
+
+ _centerPaint.SetStyle(Paint.Style.Fill);
+ _centerPaint.Color = c;
+ }
+ }
+
+ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
+ {
+ SetMeasuredDimension(CenterX * 2, CenterY * 2);
+ }
+
+ private const int CenterX = 100;
+ private const int CenterY = 100;
+ private const int CenterRadius = 32;
+
+ private static int Ave(int s, int d, float p)
+ {
+ return (int)(s + Math.Round(p * (d - s)));
+ }
+
+ private static int InterpColor(IList<int> colors, float unit)
+ {
+ if (unit <= 0)
+ {
+ return colors[0];
+ }
+ if (unit >= 1)
+ {
+ return colors[colors.Count - 1];
+ }
+
+ float p = unit * (colors.Count - 1);
+ var i = (int)p;
+ p -= i;
+
+ // now p is just the fractional part [0...1) and i is the index
+ var c0 = colors[i];
+ var c1 = colors[i + 1];
+ var a = Ave(Color.GetAlphaComponent(c0), Color.GetAlphaComponent(c1), p);
+ var r = Ave(Color.GetRedComponent(c0), Color.GetRedComponent(c1), p);
+ var g = Ave(Color.GetGreenComponent(c0), Color.GetGreenComponent(c1), p);
+ var b = Ave(Color.GetBlueComponent(c0), Color.GetBlueComponent(c1), p);
+
+ return Color.Argb(a, r, g, b);
+ }
+
+ private const float Pi = 3.1415926f;
+
+ public override bool OnTouchEvent(MotionEvent e)
+ {
+ float x = e.GetX() - CenterX;
+ float y = e.GetY() - CenterY;
+ bool inCenter = Math.Sqrt(x * x + y * y) <= CenterRadius;
+
+ switch (e.Action)
+ {
+ case MotionEventActions.Down:
+ _trackingCenter = inCenter;
+ if (inCenter)
+ {
+ _highlightCenter = true;
+ Invalidate();
+ }
+ break;
+ case MotionEventActions.Move:
+ if (_trackingCenter)
+ {
+ if (_highlightCenter != inCenter)
+ {
+ _highlightCenter = inCenter;
+ Invalidate();
+ }
+ }
+ else
+ {
+ var angle = (float)Math.Atan2(y, x);
+ // need to turn angle [-PI ... PI] into unit [0....1]
+ var unit = angle / (2 * Pi);
+ if (unit < 0)
+ {
+ unit += 1;
+ }
+ _centerPaint.Color = new Color(InterpColor(_colors, unit));
+ Invalidate();
+ }
+ break;
+ case MotionEventActions.Up:
+ if (_trackingCenter)
+ {
+ if (inCenter)
+ {
+ if (null != ColorChanged)
+ ColorChanged(this, new ColorChangedEventArgs { Color = _centerPaint.Color });
+ }
+ _trackingCenter = false; // so we draw w/o halo
+ Invalidate();
+ }
+ break;
+ }
+ return true;
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ /*if (disposing)
+ {
+ if (null != _paint)
+ {
+ _paint.Dispose();
+ }
+ if (null != _centerPaint)
+ {
+ _centerPaint.Dispose();
+ }
+ }*/
+
+ base.Dispose(disposing);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.