diff options
author | Andrei N <nitescua@yahoo.com> | 2016-10-09 10:52:38 +0300 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-11-15 11:56:03 -0800 |
commit | 4554e822e3cf5ae71724a8fc89d1a8728118f060 (patch) | |
tree | f7befe212762dd6af5e4d8ae9c90a606c5ee7fc6 /Xamarin.Forms.Platform.Android | |
parent | 0cc2fd2b6742a29fedc03de942801cc14ff6b499 (diff) | |
download | xamarin-forms-4554e822e3cf5ae71724a8fc89d1a8728118f060.tar.gz xamarin-forms-4554e822e3cf5ae71724a8fc89d1a8728118f060.tar.bz2 xamarin-forms-4554e822e3cf5ae71724a8fc89d1a8728118f060.zip |
Added CornerRadius property To Frame control
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r-- | Xamarin.Forms.Platform.Android/AppCompat/FrameRenderer.cs | 21 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs | 43 |
2 files changed, 53 insertions, 11 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/FrameRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/FrameRenderer.cs index 3ca14eae..1092f767 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/FrameRenderer.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/FrameRenderer.cs @@ -20,6 +20,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat readonly TapGestureHandler _tapGestureHandler; float _defaultElevation = -1f; + float _defaultCornerRadius = -1f; bool _clickable; bool _disposed; @@ -184,6 +185,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat e.NewElement.PropertyChanged += OnElementPropertyChanged; UpdateShadow(); UpdateBackgroundColor(); + UpdateCornerRadius(); SubscribeGestureRecognizers(e.NewElement); } } @@ -215,6 +217,8 @@ namespace Xamarin.Forms.Platform.Android.AppCompat UpdateShadow(); else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) UpdateBackgroundColor(); + else if (e.PropertyName == Frame.CornerRadiusProperty.PropertyName) + UpdateCornerRadius(); } void SubscribeGestureRecognizers(VisualElement element) @@ -286,5 +290,22 @@ namespace Xamarin.Forms.Platform.Android.AppCompat else CardElevation = 0f; } + + void UpdateCornerRadius() + { + if (_defaultCornerRadius == -1f) + { + _defaultCornerRadius = Radius; + } + + float cornerRadius = Element.CornerRadius; + + if (cornerRadius == -1f) + cornerRadius = _defaultCornerRadius; + else + cornerRadius = Context.ToPixels(cornerRadius); + + Radius = cornerRadius; + } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs index f24c8d45..ca020ad2 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs @@ -27,7 +27,10 @@ namespace Xamarin.Forms.Platform.Android base.OnElementChanged(e); if (e.NewElement != null && e.OldElement == null) + { UpdateBackground(); + UpdateCornerRadius(); + } } void UpdateBackground() @@ -35,6 +38,11 @@ namespace Xamarin.Forms.Platform.Android this.SetBackground(new FrameDrawable(Element)); } + void UpdateCornerRadius() + { + this.SetBackground(new FrameDrawable(Element)); + } + class FrameDrawable : Drawable { readonly Frame _frame; @@ -127,14 +135,13 @@ namespace Xamarin.Forms.Platform.Android using (var canvas = new ACanvas(bitmap)) { - DrawBackground(canvas, width, height, pressed); - DrawOutline(canvas, width, height); + DrawCanvas(canvas, width, height, pressed); } return bitmap; } - void DrawBackground(ACanvas canvas, int width, int height, bool pressed) + void DrawBackground(ACanvas canvas, int width, int height, float cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) @@ -142,8 +149,8 @@ namespace Xamarin.Forms.Platform.Android using (Paint.Style style = Paint.Style.Fill) using (var rect = new RectF(0, 0, width, height)) { - float rx = Forms.Context.ToPixels(5); - float ry = Forms.Context.ToPixels(5); + float rx = Forms.Context.ToPixels(cornerRadius); + float ry = Forms.Context.ToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); global::Android.Graphics.Color color = _frame.BackgroundColor.ToAndroid(); @@ -155,7 +162,7 @@ namespace Xamarin.Forms.Platform.Android } } - void DrawOutline(ACanvas canvas, int width, int height) + void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) @@ -163,8 +170,8 @@ namespace Xamarin.Forms.Platform.Android using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(0, 0, width, height)) { - float rx = Forms.Context.ToPixels(5); - float ry = Forms.Context.ToPixels(5); + float rx = Forms.Context.ToPixels(cornerRadius); + float ry = Forms.Context.ToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); paint.StrokeWidth = 1; @@ -177,19 +184,33 @@ namespace Xamarin.Forms.Platform.Android void FrameOnPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == Frame.OutlineColorProperty.PropertyName) + if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || + e.PropertyName == Frame.OutlineColorProperty.PropertyName || + e.PropertyName == Frame.CornerRadiusProperty.PropertyName) { using (var canvas = new ACanvas(_normalBitmap)) { int width = Bounds.Width(); int height = Bounds.Height(); canvas.DrawColor(global::Android.Graphics.Color.Black, PorterDuff.Mode.Clear); - DrawBackground(canvas, width, height, false); - DrawOutline(canvas, width, height); + DrawCanvas(canvas, width, height, false); } InvalidateSelf(); } } + + void DrawCanvas(ACanvas canvas, int width, int height, bool pressed) + { + float cornerRadius = _frame.CornerRadius; + + if (cornerRadius == -1f) + cornerRadius = 5f; // default corner radius + else + cornerRadius = Forms.Context.ToPixels(cornerRadius); + + DrawBackground(canvas, width, height, cornerRadius, pressed); + DrawOutline(canvas, width, height, cornerRadius); + } } } }
\ No newline at end of file |