-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPageIndicator.cs
376 lines (317 loc) · 11.1 KB
/
PageIndicator.cs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Java.Interop;
using System;
using System.Linq;
namespace Creuna.MonoDroid.PageHelper
{
public class PageIndicator : View
{
public const int NoActiveDot = -1;
public enum DotTypes
{
Single = 0,
Multiple = 1
}
private const int MinDotCount = 1;
private static readonly Rect _inRect = new Rect();
private static readonly Rect _outRect = new Rect();
private GravityFlags _gravityFlag;
private int _dotSpacing;
private Drawable _dotDrawable;
private int _dotCount;
private DotTypes _dotType;
private int _activeDot;
private int[] _extraState;
private readonly bool _initializing;
public PageIndicator(Context context) : this(context, null)
{
}
public PageIndicator(IntPtr intPtr, JniHandleOwnership jniHandleOwnership) : base(intPtr, jniHandleOwnership)
{
}
public PageIndicator(Context context, IAttributeSet attributeSet) : this(context, attributeSet, Resource.Attribute.gdPageIndicatorStyle)
{
}
public PageIndicator(Context context, IAttributeSet attributeSet, int defStyle) : base(context, attributeSet, defStyle)
{
InitPageIndicator();
_initializing = true;
var a = context.ObtainStyledAttributes(attributeSet, Resource.Styleable.PageIndicator, defStyle, 0);
DotCount = a.GetInt(Resource.Styleable.PageIndicator_dotCount, DotCount);
ActiveDot = a.GetInt(Resource.Styleable.PageIndicator_activeDot, _activeDot);
DotDrawable = a.GetDrawable(Resource.Styleable.PageIndicator_dotDrawable);
DotSpacing = a.GetDimensionPixelSize(Resource.Styleable.PageIndicator_dotSpacing, _dotSpacing);
GravityFlag = (GravityFlags) a.GetInt(Resource.Styleable.PageIndicator_gravity, (int) _gravityFlag);
DotType = (DotTypes) a.GetInt(Resource.Styleable.PageIndicator_dotType, (int) _dotType);
a.Recycle();
_initializing = false;
}
private void InitPageIndicator()
{
DotCount = MinDotCount;
_gravityFlag = GravityFlags.Center;
_activeDot = 0;
_dotSpacing = 0;
_dotType = DotTypes.Single;
_extraState = OnCreateDrawableState(1);
var setAsInt = SelectedStateSet.OfType<int>().ToArray();
MergeDrawableStates(_extraState, setAsInt);
}
public int DotCount
{
get { return _dotCount; }
set
{
if (value < MinDotCount)
value = MinDotCount;
if (_dotCount != value)
{
_dotCount = value;
RequestLayout();
Invalidate();
}
}
}
public int ActiveDot
{
get { return _activeDot; }
set
{
if (value < 0)
{
value = NoActiveDot;
}
switch (_dotType)
{
case DotTypes.Single:
if (value > _dotCount - 1)
{
value = NoActiveDot;
}
break;
case DotTypes.Multiple:
if (value > _dotCount)
{
value = NoActiveDot;
}
break;
}
_activeDot = value;
Invalidate();
}
}
public Drawable DotDrawable
{
get { return _dotDrawable; }
set
{
if (value != _dotDrawable)
{
if (_dotDrawable != null)
{
_dotDrawable.SetCallback(null);
}
}
_dotDrawable = value;
if (value != null)
{
if (value.IntrinsicHeight == -1 || value.IntrinsicWidth == -1)
{
return;
}
value.SetBounds(0, 0, value.IntrinsicWidth, value.IntrinsicHeight);
value.SetCallback(this);
if (value.IsStateful)
{
value.SetState(GetDrawableState());
}
}
RequestLayout();
Invalidate();
}
}
public int DotSpacing
{
get { return _dotSpacing; }
set
{
if (value != _dotSpacing)
{
_dotSpacing = value;
RequestLayout();
Invalidate();
}
}
}
public GravityFlags GravityFlag
{
get { return _gravityFlag; }
set
{
if (_gravityFlag != value)
{
_gravityFlag = value;
Invalidate();
}
}
}
public DotTypes DotType
{
get { return _dotType; }
set
{
if (value == DotTypes.Single || value == DotTypes.Multiple)
{
if (_dotType != value)
{
_dotType = value;
Invalidate();
}
}
}
}
public override void RequestLayout()
{
if (!_initializing)
{
base.RequestLayout();
}
}
public override void Invalidate()
{
if (!_initializing)
{
base.Invalidate();
}
}
protected override bool VerifyDrawable(Drawable who)
{
return base.VerifyDrawable(who) || who == _dotDrawable;
}
protected override void DrawableStateChanged()
{
base.DrawableStateChanged();
_extraState = OnCreateDrawableState(1);
var setAsInt = SelectedStateSet.OfType<int>().ToArray();
MergeDrawableStates(_extraState, setAsInt);
Invalidate();
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var d = _dotDrawable;
int width = 0;
int height = 0;
if (d != null)
{
width = _dotCount*(d.IntrinsicWidth + _dotSpacing) - _dotSpacing;
height = d.IntrinsicHeight;
}
width += PaddingRight + PaddingLeft;
height += PaddingBottom + PaddingTop;
SetMeasuredDimension(ResolveSize(width, widthMeasureSpec), ResolveSize(height, heightMeasureSpec));
}
protected override void OnDraw(Canvas canvas)
{
var d = _dotDrawable;
if (d != null)
{
var count = _dotType == DotTypes.Single ? _dotCount : _activeDot;
if (count <= 0) return;
int h = d.IntrinsicHeight;
int w = Math.Max(0, count*(d.IntrinsicWidth + _dotSpacing) - _dotSpacing);
int pRight = PaddingRight;
int pLeft = PaddingLeft;
int pTop = PaddingTop;
int pBottom = PaddingBottom;
_inRect.Set(pLeft, pTop, Width - pRight, Height - pBottom);
Gravity.Apply(_gravityFlag, w, h, _inRect, _outRect);
canvas.Save();
canvas.Translate(_outRect.Left, _outRect.Top);
for (int i = 0; i < count; i++)
{
if (d.IsStateful)
{
int[] state = GetDrawableState();
if (_dotType == DotTypes.Multiple || i == _activeDot)
{
state = _extraState;
}
d.SetCallback(null);
d.SetState(state);
d.SetCallback(this);
}
d.Draw(canvas);
canvas.Translate(_dotSpacing + d.IntrinsicWidth, 0);
}
canvas.Restore();
}
}
protected class SavedState : BaseSavedState
{
public int ActiveDot { get; set; }
public SavedState(IParcelable superState) : base(superState)
{
}
public SavedState(Parcel into) : base(into)
{
ActiveDot = into.ReadInt();
}
public override void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
{
base.WriteToParcel(dest, flags);
dest.WriteInt(ActiveDot);
}
private static readonly GenericParcelableCreator<SavedState> _creator = new GenericParcelableCreator<SavedState>((parcel => new SavedState(parcel)));
[ExportField("CREATOR")]
public static GenericParcelableCreator<SavedState> GetCreator()
{
return _creator;
}
public SavedState() : base(EmptyState)
{
}
}
public sealed class GenericParcelableCreator<T> : Java.Lang.Object, IParcelableCreator where T : Java.Lang.Object, new()
{
private readonly Func<Parcel, T> _createFunc;
/// <summary>
/// Initializes a new instance of the <see cref="ParcelableDemo.GenericParcelableCreator`1"/> class.
/// </summary>
/// <param name='createFromParcelFunc'>
/// Func that creates an instance of T, populated with the values from the parcel parameter
/// </param>
public GenericParcelableCreator(Func<Parcel, T> createFromParcelFunc)
{
_createFunc = createFromParcelFunc;
}
#region IParcelableCreator Implementation
public Java.Lang.Object CreateFromParcel(Parcel source)
{
return _createFunc(source);
}
public Java.Lang.Object[] NewArray(int size)
{
return new T[size];
}
#endregion
}
protected override IParcelable OnSaveInstanceState()
{
var superState = base.OnSaveInstanceState();
var ss = new SavedState(superState) {ActiveDot = _activeDot};
return ss;
}
protected override void OnRestoreInstanceState(IParcelable state)
{
var ss = (SavedState) state;
base.OnRestoreInstanceState(ss.SuperState);
_activeDot = ss.ActiveDot;
}
}
}