summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs
blob: 921c65498df18e6523291b89160b9db3bedd358f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System.ComponentModel;
using Android.Graphics;
using Android.Graphics.Drawables;
using AButton = Android.Widget.Button;
using ACanvas = Android.Graphics.Canvas;
using GlobalResource = Android.Resource;

namespace Xamarin.Forms.Platform.Android
{
	public class FrameRenderer : VisualElementRenderer<Frame>
	{
		bool _disposed;

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);

			if (disposing && !_disposed)
			{
				Background.Dispose();
				_disposed = true;
			}
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
		{
			base.OnElementChanged(e);

			if (e.NewElement != null && e.OldElement == null)
			{
				UpdateBackground();
				UpdateCornerRadius();
			}
		}

		void UpdateBackground()
		{
			this.SetBackground(new FrameDrawable(Element));
		}

		void UpdateCornerRadius()
		{
			this.SetBackground(new FrameDrawable(Element));
		}

		class FrameDrawable : Drawable
		{
			readonly Frame _frame;

			bool _isDisposed;
			Bitmap _normalBitmap;

			public FrameDrawable(Frame frame)
			{
				_frame = frame;
				frame.PropertyChanged += FrameOnPropertyChanged;
			}

			public override bool IsStateful
			{
				get { return false; }
			}

			public override int Opacity
			{
				get { return 0; }
			}

			public override void Draw(ACanvas canvas)
			{
				int width = Bounds.Width();
				int height = Bounds.Height();

				if (width <= 0 || height <= 0)
				{
					if (_normalBitmap != null)
					{
						_normalBitmap.Dispose();
						_normalBitmap = null;
					}
					return;
				}

				if (_normalBitmap == null || _normalBitmap.Height != height || _normalBitmap.Width != width)
				{
					// If the user changes the orientation of the screen, make sure to destroy reference before
					// reassigning a new bitmap reference.
					if (_normalBitmap != null)
					{
						_normalBitmap.Dispose();
						_normalBitmap = null;
					}

					_normalBitmap = CreateBitmap(false, width, height);
				}
				Bitmap bitmap = _normalBitmap;
				using (var paint = new Paint())
					canvas.DrawBitmap(bitmap, 0, 0, paint);
			}

			public override void SetAlpha(int alpha)
			{
			}

			public override void SetColorFilter(ColorFilter cf)
			{
			}

			protected override void Dispose(bool disposing)
			{
				if (disposing && !_isDisposed)
				{
					if (_normalBitmap != null)
					{
						_normalBitmap.Dispose();
						_normalBitmap = null;
					}

					_isDisposed = true;
				}

				base.Dispose(disposing);
			}

			protected override bool OnStateChange(int[] state)
			{
				return false;
			}

			Bitmap CreateBitmap(bool pressed, int width, int height)
			{
				Bitmap bitmap;
				using (Bitmap.Config config = Bitmap.Config.Argb8888)
					bitmap = Bitmap.CreateBitmap(width, height, config);

				using (var canvas = new ACanvas(bitmap))
				{
					DrawCanvas(canvas, width, height, pressed);
				}

				return bitmap;
			}

			void DrawBackground(ACanvas canvas, int width, int height, float cornerRadius, bool pressed)
			{
				using (var paint = new Paint { AntiAlias = true })
				using (var path = new Path())
				using (Path.Direction direction = Path.Direction.Cw)
				using (Paint.Style style = Paint.Style.Fill)
				using (var rect = new RectF(0, 0, width, height))
				{
					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();

					paint.SetStyle(style);
					paint.Color = color;

					canvas.DrawPath(path, paint);
				}
			}

			void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius)
			{
				using (var paint = new Paint { AntiAlias = true })
				using (var path = new Path())
				using (Path.Direction direction = Path.Direction.Cw)
				using (Paint.Style style = Paint.Style.Stroke)
				using (var rect = new RectF(0, 0, width, height))
				{
					float rx = Forms.Context.ToPixels(cornerRadius);
					float ry = Forms.Context.ToPixels(cornerRadius);
					path.AddRoundRect(rect, rx, ry, direction);

					paint.StrokeWidth = 1;
					paint.SetStyle(style);
					paint.Color = _frame.OutlineColor.ToAndroid();

					canvas.DrawPath(path, paint);
				}
			}

			void FrameOnPropertyChanged(object sender, PropertyChangedEventArgs e)
			{
				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);
						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);
			}
		}
	}
}