diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-09-08 20:39:05 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-09-08 11:39:05 -0700 |
commit | 85426c5d9495eb1d55b3128bf97e50c68a73b53f (patch) | |
tree | 2f81e5868ce61eb90d15c6c51a354603b8395627 /Xamarin.Forms.ControlGallery.Android | |
parent | 11326e1c182b3ff5c3d82c6ef7d09c193bc19891 (diff) | |
download | xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.tar.gz xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.tar.bz2 xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.zip |
Native Bindings (#278)
* [C, I, A, W] Support Native Bindings
* fix tabs
Diffstat (limited to 'Xamarin.Forms.ControlGallery.Android')
3 files changed, 222 insertions, 0 deletions
diff --git a/Xamarin.Forms.ControlGallery.Android/Activity1.cs b/Xamarin.Forms.ControlGallery.Android/Activity1.cs index bc1772c6..35f877dc 100644 --- a/Xamarin.Forms.ControlGallery.Android/Activity1.cs +++ b/Xamarin.Forms.ControlGallery.Android/Activity1.cs @@ -20,6 +20,7 @@ using System.IO; using System.IO.IsolatedStorage; using Droid = Android; +using System.Globalization; [assembly: Dependency (typeof (CacheService))] @@ -337,6 +338,14 @@ namespace Xamarin.Forms.ControlGallery.Android if (nncgPage != null) { AddNativeControls (nncgPage); } + + var nncgPage1 = args.Page as NativeBindingGalleryPage; + + if (nncgPage1 != null) + { + AddNativeBindings(nncgPage1); + } + }; } @@ -408,6 +417,58 @@ namespace Xamarin.Forms.ControlGallery.Android var size = new Size (nativeView.MeasuredWidth, nativeView.MeasuredHeight); return new SizeRequest (size); } + + void AddNativeBindings(NativeBindingGalleryPage page) + { + if (page.NativeControlsAdded) + return; + + StackLayout sl = page.Layout; + + var textView = new TextView(this) + { + TextSize = 14, + Text = "This will be text" + }; + + var viewGroup = new LinearLayout(this); + viewGroup.AddView(textView); + + var buttonColor = new global::Android.Widget.Button(this) { Text = "Change label Color" }; + buttonColor.Click += (sender, e) => textView.SetTextColor(Color.Blue.ToAndroid()); + + var colorPicker = new ColorPickerView(this, 200, 200); + + textView.SetBinding(nameof(textView.Text), new Binding("NativeLabel")); + //this doesn't work because there's not TextColor property + //textView.SetBinding("TextColor", new Binding("NativeLabelColor", converter: new ColorConverter())); + colorPicker.SetBinding(nameof(colorPicker.SelectedColor), new Binding("NativeLabelColor", BindingMode.TwoWay, new ColorConverter()), "ColorPicked"); + + sl?.Children.Add(viewGroup); + sl?.Children.Add(buttonColor.ToView()); + sl?.Children.Add(colorPicker); + + page.NativeControlsAdded = true; + } + + public class ColorConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is Color) + return ((Color)value).ToAndroid(); + + return null; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is global::Android.Graphics.Color) + return ((global::Android.Graphics.Color)value).ToColor(); + + return null; + } + } } #endif } diff --git a/Xamarin.Forms.ControlGallery.Android/ColorPicker.cs b/Xamarin.Forms.ControlGallery.Android/ColorPicker.cs new file mode 100644 index 00000000..8e047870 --- /dev/null +++ b/Xamarin.Forms.ControlGallery.Android/ColorPicker.cs @@ -0,0 +1,160 @@ +using System; +using System.ComponentModel; +using Android.Content; +using Android.Views; +using Android.Widget; +using Xamarin.Forms.Platform.Android; +using Droid = Android; + +namespace Xamarin.Forms.ControlGallery.Android +{ + public class ColorPickerView : ViewGroup + //, INotifyPropertyChanged + { + static readonly int[] COLORS = new[] { + new Droid.Graphics.Color(255,0,0,255).ToArgb(), new Droid.Graphics.Color(255,0,255,255).ToArgb(), new Droid.Graphics.Color(0,0,255,255).ToArgb(), + new Droid.Graphics.Color(0,255,255,255).ToArgb(), new Droid.Graphics.Color(0,255,0,255).ToArgb(), new Droid.Graphics.Color(255,255,0,255).ToArgb(), + new Droid.Graphics.Color(255,0,0,255).ToArgb() + }; + Droid.Graphics.Point currentPoint; + ColorPointer colorPointer; + ImageView imageViewSelectedColor; + ImageView imageViewPallete; + Droid.Graphics.Color selectedColor; + Droid.Graphics.Color previewColor; + + //public event PropertyChangedEventHandler PropertyChanged; + public event EventHandler ColorPicked; + + public ColorPickerView(Context context, int minWidth, int minHeight) : base(context) + { + SelectedColor = Color.Black.ToAndroid(); + + SetMinimumHeight(minHeight); + SetMinimumWidth(minWidth); + + imageViewPallete = new ImageView(context); + imageViewPallete.DrawingCacheEnabled = true; + imageViewPallete.Background = new Droid.Graphics.Drawables.GradientDrawable(Droid.Graphics.Drawables.GradientDrawable.Orientation.LeftRight, COLORS); + + imageViewPallete.Touch += (object sender, TouchEventArgs e) => + { + if (e.Event.Action == MotionEventActions.Down || e.Event.Action == MotionEventActions.Move) + { + currentPoint = new Droid.Graphics.Point((int)e.Event.GetX(), (int)e.Event.GetY()); + + previewColor = GetCurrentColor(imageViewPallete.GetDrawingCache(false), (int)e.Event.GetX(), (int)e.Event.GetY()); + } + if (e.Event.Action == MotionEventActions.Up) + { + SelectedColor = previewColor; + } + }; + + imageViewSelectedColor = new ImageView(context); + colorPointer = new ColorPointer(context); + + AddView(imageViewPallete); + AddView(imageViewSelectedColor); + AddView(colorPointer); + } + + public Droid.Graphics.Color SelectedColor + { + get + { + return selectedColor; + } + + set + { + if (selectedColor == value) + return; + + selectedColor = value; + UpdateUi(); + OnPropertyChanged(); + OnColorPicked(); + } + } + + protected override void OnLayout(bool changed, int left, int top, int right, int bottom) + { + var half = (bottom - top) / 2; + var margin = 20; + + var palleteY = top + half; + + imageViewSelectedColor.Layout(left, top, right, bottom - half - margin); + imageViewPallete.Layout(left, palleteY, right, bottom); + colorPointer.Layout(left, palleteY, right, bottom); + } + + void UpdateUi() + { + imageViewSelectedColor?.SetBackgroundColor(selectedColor); + colorPointer?.UpdatePoint(currentPoint); + } + + Droid.Graphics.Color GetCurrentColor(Droid.Graphics.Bitmap bitmap, int x, int y) + { + if (bitmap == null) + return new Droid.Graphics.Color(255, 255, 255, 255); + + if (x < 0) + x = 0; + if (y < 0) + y = 0; + if (x >= bitmap.Width) + x = bitmap.Width - 1; + if (y >= bitmap.Height) + y = bitmap.Height - 1; + + int color = bitmap.GetPixel(x, y); + return new Droid.Graphics.Color(color); + } + + void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) + { + //PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + void OnColorPicked() + { + ColorPicked?.Invoke(this, new EventArgs()); + } + } + + public class ColorPointer : Droid.Views.View + { + Droid.Graphics.Paint colorPointerPaint; + Droid.Graphics.Point currentPoint; + Droid.Graphics.Point nextPoint; + + public ColorPointer(Context context) : base(context) + { + + colorPointerPaint = new Droid.Graphics.Paint(); + colorPointerPaint.SetStyle(Droid.Graphics.Paint.Style.Stroke); + colorPointerPaint.StrokeWidth = 5f; + colorPointerPaint.SetARGB(255, 0, 0, 0); + + } + + public void UpdatePoint(Droid.Graphics.Point p) + { + if (p == null) + return; + + if (currentPoint == null) + currentPoint = nextPoint; + + nextPoint = p; + } + + protected override void OnDraw(Droid.Graphics.Canvas canvas) + { + base.OnDraw(canvas); + } + } +} diff --git a/Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj b/Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj index 25f896f8..4a2e5288 100644 --- a/Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj +++ b/Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj @@ -175,6 +175,7 @@ <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Resources\Resource.Designer.cs" /> <Compile Include="CustomRenderers.cs" /> + <Compile Include="ColorPicker.cs" /> </ItemGroup> <ItemGroup> <AndroidAsset Include="Assets\default.css" /> |