Permalink
Browse files

First sample.

  • Loading branch information...
1 parent 989175f commit de4f4daf01aef709c436c3f1c0b4959252eee659 @Cheesebaron committed Jun 24, 2012
View
@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoDroid.ColorPickers", "MonoDroid.ColorPickers\MonoDroid.ColorPickers.csproj", "{4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Release|Any CPU.Build.0 = Release|Any CPU
+ {4FBCBB3F-498C-484A-B043-EDC7FD98FE1E}.Release|Any CPU.Deploy.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,42 @@
+using System;
+using Android.App;
+using Android.Graphics;
+using Android.Views;
+using Android.Widget;
+using Android.OS;
+
+namespace MonoDroid.ColorPickers
+{
+ [Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon")]
+ public class Activity1 : Activity
+ {
+ 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);
+
+ var colorPickerAlpha = new ColorPickerDialog(this, Color.Aqua);
+ colorPickerAlpha.ColorChanged += (sender, args) => Console.WriteLine("Color changed: {0}", args.Color);
+ colorPickerAlpha.AlphaSliderVisible = true;
+
+ var ll = new LinearLayout(this)
+ {
+ LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FillParent,
+ ViewGroup.LayoutParams.FillParent)
+ };
+
+ var btColorPicker = new Button(this) { Text = "Show Color Picker" };
+ btColorPicker.Click += (sender, args) => colorPicker.Show();
+
+ var btColorPickerAlpha = new Button(this) { Text = "Show Color Picker (Alpha)" };
+ btColorPickerAlpha.Click += (sender, args) => colorPickerAlpha.Show();
+
+ ll.AddView(btColorPicker);
+ ll.AddView(btColorPickerAlpha);
+ SetContentView(ll);
+ }
+ }
+}
+
@@ -0,0 +1,123 @@
+/*
+ * Direct port to Mono for Android of https://github.com/attenzione/android-ColorPickerPreference
+ * by Tomasz Cielecki <tomasz@ostebaronen.dk>
+ * Whose license is:
+ *
+ * Copyright (C) 2010 Daniel Nilsson
+ *
+ * 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.Graphics;
+using Android.Graphics.Drawables;
+
+namespace MonoDroid.ColorPickers
+{
+ public class AlphaPatternDrawable: Drawable
+ {
+ private readonly int _rectangleSize = 10;
+
+ private readonly Paint _paint = new Paint();
+ private readonly Paint _paintWhite = new Paint();
+ private readonly Paint _paintGray = new Paint();
+
+ private int _numRectanglesHorizontal;
+ private int _numRectanglesVertical;
+
+ /**
+ * Bitmap in which the pattern will be cahched.
+ */
+ private Bitmap _bitmap;
+
+ public AlphaPatternDrawable(int rectangleSize)
+ {
+ _rectangleSize = rectangleSize;
+ _paintWhite.Color = Color.Argb(255,255,255,255);
+ _paintGray.Color = Color.Argb(255,203,203,203);
+ }
+
+ public override void Draw(Canvas canvas)
+ {
+ canvas.DrawBitmap(_bitmap, null, Bounds, _paint);
+ }
+
+ protected override void OnBoundsChange(Rect bounds)
+ {
+ base.OnBoundsChange(bounds);
+
+ var height = Bounds.Height();
+ var width = Bounds.Width();
+
+ _numRectanglesHorizontal = (int)Math.Ceiling((double) (width / _rectangleSize));
+ _numRectanglesVertical = (int)Math.Ceiling((double) (height / _rectangleSize));
+
+ GeneratePatternBitmap();
+ }
+
+ /**
+ * This will generate a bitmap with the pattern
+ * as big as the rectangle we were allow to draw on.
+ * We do this to chache the bitmap so we don't need to
+ * recreate it each time draw() is called since it
+ * takes a few milliseconds.
+ */
+ private void GeneratePatternBitmap()
+ {
+
+ if (Bounds.Width() <= 0 || Bounds.Height() <= 0)
+ {
+ return;
+ }
+
+ _bitmap = Bitmap.CreateBitmap(Bounds.Width(), Bounds.Height(), Bitmap.Config.Argb8888);
+ var canvas = new Canvas(_bitmap);
+
+ var r = new Rect();
+ var verticalStartWhite = true;
+ for (var i = 0; i <= _numRectanglesVertical; i++)
+ {
+ var isWhite = verticalStartWhite;
+ for (var j = 0; j <= _numRectanglesHorizontal; j++)
+ {
+
+ r.Top = i * _rectangleSize;
+ r.Left = j * _rectangleSize;
+ r.Bottom = r.Top + _rectangleSize;
+ r.Right = r.Left + _rectangleSize;
+
+ canvas.DrawRect(r, isWhite ? _paintWhite : _paintGray);
+
+ isWhite = !isWhite;
+ }
+
+ verticalStartWhite = !verticalStartWhite;
+ }
+ }
+
+ public override void SetAlpha(int alpha)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override void SetColorFilter(ColorFilter cf)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override int Opacity
+ {
+ get { return 0; }
+ }
+ }
+}
@@ -0,0 +1,19 @@
+Any raw assets you want to be deployed with your application can be placed in
+this directory (and child directories) and given a Build Action of "AndroidAsset".
+
+These files will be deployed with you package and will be accessible using Android's
+AssetManager, like this:
+
+public class ReadAsset : Activity
+{
+ protected override void OnCreate (Bundle bundle)
+ {
+ base.OnCreate (bundle);
+
+ InputStream input = Assets.Open ("my_asset.txt");
+ }
+}
+
+Additionally, some Android functions will automatically load asset files:
+
+Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
@@ -0,0 +1,134 @@
+/*
+ * Direct port to Mono for Android of https://github.com/attenzione/android-ColorPickerPreference
+ * by Tomasz Cielecki <tomasz@ostebaronen.dk>
+ * Whose license is:
+ *
+ * Copyright (C) 2010 Daniel Nilsson
+ *
+ * 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;
+using Android.Views;
+using Android.Widget;
+
+namespace MonoDroid.ColorPickers
+{
+ public class ColorPickerDialog: Dialog, View.IOnClickListener
+ {
+ public event ColorChangedEventHandler ColorChanged;
+
+ private ColorPickerView _colorPicker;
+
+ private ColorPickerPanelView _oldColor;
+ private ColorPickerPanelView _newColor;
+
+ public Color Color
+ {
+ get { return _colorPicker.Color; }
+ }
+
+ public bool AlphaSliderVisible
+ {
+ get { return _colorPicker.AlphaSliderVisible; }
+ set { _colorPicker.AlphaSliderVisible = value; }
+ }
+
+ protected ColorPickerDialog(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
+ {
+ Init(Color.BlanchedAlmond);
+ }
+
+ public ColorPickerDialog(Context context, Color initialColor) : base(context)
+ {
+ Init(initialColor);
+ }
+
+ private void Init(Color color)
+ {
+ // To fight color banding.
+ Window.SetFormat(Format.Rgba8888);
+
+ SetUp(color);
+ }
+
+ private void SetUp(Color color)
+ {
+ var inflater = (LayoutInflater) Context.GetSystemService(Context.LayoutInflaterService);
+
+ var layout = inflater.Inflate(Resource.Layout.dialog_color_picker, null);
+
+ SetContentView(layout);
+
+ SetTitle(Resource.String.dialog_color_picker);
+
+ _colorPicker = layout.FindViewById<ColorPickerView>(Resource.Id.color_picker_view);
+ _oldColor = layout.FindViewById<ColorPickerPanelView>(Resource.Id.old_color_panel);
+ _newColor = layout.FindViewById<ColorPickerPanelView>(Resource.Id.new_color_panel);
+
+ ((LinearLayout)_oldColor.Parent).SetPadding(
+ (int) Math.Round(_colorPicker.DrawingOffset),
+ 0,
+ (int) Math.Round(_colorPicker.DrawingOffset),
+ 0
+ );
+
+ _oldColor.SetOnClickListener(this);
+ _newColor.SetOnClickListener(this);
+ _colorPicker.ColorChanged += (sender, args) =>
+ {
+ _newColor.Color = args.Color;
+ if (ColorChanged != null)
+ ColorChanged(this, new ColorEventArgs { Color = _newColor.Color });
+ };
+ _oldColor.Color = color;
+ _colorPicker.Color = color;
+ }
+
+ public void OnClick(View v)
+ {
+ switch (v.Id)
+ {
+ case Resource.Id.new_color_panel:
+ if (ColorChanged != null)
+ ColorChanged(this, new ColorEventArgs { Color = _newColor.Color });
+ break;
+ case Resource.Id.old_color_panel:
+ if (ColorChanged != null)
+ ColorChanged(this, new ColorEventArgs { Color = _oldColor.Color });
+ break;
+ }
+ Dismiss();
+ }
+
+ public override Bundle OnSaveInstanceState()
+ {
+ var state = base.OnSaveInstanceState();
+ state.PutInt("old_color", _oldColor.Color);
+ state.PutInt("new_color", _newColor.Color);
+ return state;
+ }
+
+ public override void OnRestoreInstanceState(Bundle savedInstanceState)
+ {
+ _oldColor.Color = new Color(savedInstanceState.GetInt("old_color"));
+ _colorPicker.Color = new Color(savedInstanceState.GetInt("new_color"));
+ base.OnRestoreInstanceState(savedInstanceState);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit de4f4da

Please sign in to comment.