Permalink
Browse files

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

…le Activity.
  • Loading branch information...
1 parent de4f4da commit e8c176cee02edeee88bd9e5bf6175bd105ba46e5 @Cheesebaron committed Jul 3, 2012
View
@@ -3,4 +3,7 @@ bin
obj
# mstest test results
-TestResults
+TestResults
+
+# ReSharper
+_ReSharper.*
View
Binary file not shown.
@@ -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();
+ }
}
}
}
@@ -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; }
+ }
+}
@@ -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();
@@ -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);
@@ -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);
+ }
}
}
@@ -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" />
@@ -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>
Oops, something went wrong.

0 comments on commit e8c176c

Please sign in to comment.