summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs
blob: 62eaecbf1d38e7d2c97b25e7c5ec0f7ab222621d (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
using System.Linq;
using Android.Graphics;
using Android.Graphics.Drawables;

namespace Xamarin.Forms.Platform.Android
{
	internal class ButtonDrawable : Drawable
	{
		bool _isDisposed;
		Bitmap _normalBitmap;
		bool _pressed;
		Bitmap _pressedBitmap;

		public ButtonDrawable()
		{
			_pressed = false;
		}

		public Button Button { get; set; }

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

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

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

			if (width <= 0 || height <= 0)
				return;

			if (_normalBitmap == null || _normalBitmap.Height != height || _normalBitmap.Width != width)
			{
				Reset();

				_normalBitmap = CreateBitmap(false, width, height);
				_pressedBitmap = CreateBitmap(true, width, height);
			}

			Bitmap bitmap = GetState().Contains(global::Android.Resource.Attribute.StatePressed) ? _pressedBitmap : _normalBitmap;
			canvas.DrawBitmap(bitmap, 0, 0, new Paint());
		}

		public void Reset()
		{
			if (_normalBitmap != null)
			{
				_normalBitmap.Recycle();
				_normalBitmap.Dispose();
				_normalBitmap = null;
			}

			if (_pressedBitmap != null)
			{
				_pressedBitmap.Recycle();
				_pressedBitmap.Dispose();
				_pressedBitmap = null;
			}
		}

		public override void SetAlpha(int alpha)
		{
		}

		public override void SetColorFilter(ColorFilter cf)
		{
		}

		protected override void Dispose(bool disposing)
		{
			if (_isDisposed)
				return;

			_isDisposed = true;

			if (disposing)
				Reset();

			base.Dispose(disposing);
		}

		protected override bool OnStateChange(int[] state)
		{
			bool old = _pressed;
			_pressed = state.Contains(global::Android.Resource.Attribute.StatePressed);
			if (_pressed != old)
			{
				InvalidateSelf();
				return true;
			}
			return false;
		}

		Bitmap CreateBitmap(bool pressed, int width, int height)
		{
			Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
			using (var canvas = new Canvas(bitmap))
			{
				DrawBackground(canvas, width, height, pressed);
				DrawOutline(canvas, width, height);
			}

			return bitmap;
		}

		void DrawBackground(Canvas canvas, int width, int height, bool pressed)
		{
			var paint = new Paint { AntiAlias = true };
			var path = new Path();

			float borderRadius = Forms.Context.ToPixels(Button.BorderRadius);

			path.AddRoundRect(new RectF(0, 0, width, height), borderRadius, borderRadius, Path.Direction.Cw);

			paint.Color = pressed ? Button.BackgroundColor.AddLuminosity(-0.1).ToAndroid() : Button.BackgroundColor.ToAndroid();
			paint.SetStyle(Paint.Style.Fill);
			canvas.DrawPath(path, paint);
		}

		void DrawOutline(Canvas canvas, int width, int height)
		{
			if (Button.BorderWidth <= 0)
				return;

			using (var paint = new Paint { AntiAlias = true })
			using (var path = new Path())
			{
				float borderWidth = Forms.Context.ToPixels(Button.BorderWidth);
				float inset = borderWidth / 2;

				// adjust border radius so outer edge of stroke is same radius as border radius of background
				float borderRadius = Forms.Context.ToPixels(Button.BorderRadius) - inset;

				path.AddRoundRect(new RectF(inset, inset, width - inset, height - inset), borderRadius, borderRadius, Path.Direction.Cw);
				paint.StrokeWidth = borderWidth;
				paint.SetStyle(Paint.Style.Stroke);
				paint.Color = Button.BorderColor.ToAndroid();

				canvas.DrawPath(path, paint);
			}
		}
	}
}