-
Notifications
You must be signed in to change notification settings - Fork 0
/
Switch.java
400 lines (377 loc) · 11.4 KB
/
Switch.java
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package stan.rgcc.demo.ui.views;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import stan.rgcc.demo.R;
public class Switch
extends View
{
private int textColor;
private int circleColor;
private int innerCircleColor;
private String leftText = "";
private int textHeight;
private int leftTextWidth;
private int leftWidth;
private int leftX;
private int innerCircleX;
private int leftTextX;
private String rightText = "";
private int rightTextWidth;
private int rightX;
private int rightTextX;
private int textSize;
private int circleSize;
private int circleStrokeWidth;
private int innerCircleSize;
private int maxInnerCircleSize;
private int animateDuration;
private Interpolator interpolator;
private int side;
private int oldSide;
private AnimatorSet currentAnimation;
private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint innerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Animator.AnimatorListener leftToRightProxy = new Animator.AnimatorListener()
{
@Override
public void onAnimationStart(Animator animator)
{
}
@Override
public void onAnimationEnd(Animator animator)
{
post(new Runnable()
{
@Override
public void run()
{
innerCircleX = rightX;
moveSwitch(0, maxInnerCircleSize, animateDuration, null);
}
});
}
@Override
public void onAnimationCancel(Animator animator)
{
}
@Override
public void onAnimationRepeat(Animator animator)
{
}
};
private final Animator.AnimatorListener rightToLeftProxy = new Animator.AnimatorListener()
{
@Override
public void onAnimationStart(Animator animator)
{
}
@Override
public void onAnimationEnd(Animator animator)
{
post(new Runnable()
{
@Override
public void run()
{
innerCircleX = leftX;
moveSwitch(0, maxInnerCircleSize, animateDuration, null);
}
});
}
@Override
public void onAnimationCancel(Animator animator)
{
}
@Override
public void onAnimationRepeat(Animator animator)
{
}
};
private ChangeSideListener listener;
public Switch(Context context, AttributeSet attrs)
{
super(context, attrs);
textPaint.setStyle(Paint.Style.FILL);
circlePaint.setStyle(Paint.Style.STROKE);
circleStrokeWidth = 2;
innerCirclePaint.setStyle(Paint.Style.FILL);
TypedArray switchTypedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.Switch,
0, 0);
try
{
setLeftText(switchTypedArray.getString(R.styleable.Switch_left_text));
setRightText(switchTypedArray.getString(R.styleable.Switch_right_text));
setTextSize(switchTypedArray.getDimensionPixelSize(R.styleable.Switch_text_size, 0));
setTextColor(switchTypedArray.getColor(R.styleable.Switch_text_color, Color.BLACK));
setInnerCircleColor(switchTypedArray.getColor(R.styleable.Switch_inner_circle_color, Color.BLACK));
setCircleColor(switchTypedArray.getColor(R.styleable.Switch_outer_circle_color, Color.BLACK));
setCircleSize(switchTypedArray.getDimensionPixelSize(R.styleable.Switch_inner_circle_radius, 0));
setAnimateDuration(switchTypedArray.getInt(R.styleable.Switch_animate_duration, 50));
}
finally
{
switchTypedArray.recycle();
}
interpolator = new AccelerateDecelerateInterpolator();
side = Sides.NOTHING;
oldSide = Sides.NOTHING;
setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
}
});
recalculate();
}
private void setAnimateDuration(int time)
{
if(time < 50)
{
time = 50;
}
else if(time > 300)
{
time = 300;
}
animateDuration = time;
}
@Override
protected void onDraw(Canvas canvas)
{
int h = getHeight();
int textY = h - (h-textHeight)/2;
canvas.drawCircle(leftX, h/2, circleSize, circlePaint);
if(side == Sides.LEFT || side == Sides.RIGHT)
{
canvas.drawCircle(innerCircleX, h/2, innerCircleSize, innerCirclePaint);
}
canvas.drawText(leftText, leftTextX, textY, textPaint);
canvas.drawCircle(rightX, h/2, circleSize, circlePaint);
canvas.drawText(rightText, rightTextX, textY, textPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = rightTextX+rightTextWidth - (leftX-circleSize) + circleStrokeWidth*2;
int height = circleSize*2 + circleStrokeWidth*2;
setMeasuredDimension(width, height);
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int masked = ev.getActionMasked();
switch(masked)
{
case MotionEvent.ACTION_DOWN:
{
touch(ev.getX(), ev.getY());
break;
}
case MotionEvent.ACTION_MOVE:
{
touch(ev.getX(), ev.getY());
break;
}
case MotionEvent.ACTION_UP:
{
if(listener != null && side != oldSide)
{
oldSide = side;
listener.changeSide(side);
}
break;
}
}
return super.onTouchEvent(ev);
// return true;
}
private void touch(float x, float y)
{
// Log.e(getClass().getName(), "touch " + x + " " + y);
if(x < leftWidth + circleSize/2)
{
setSide(Sides.LEFT);
}
else
{
setSide(Sides.RIGHT);
}
}
public void setInnerCircleSize(int value)
{
innerCircleSize = value;
// Log.e(getClass().getName(), "innerCircleSize " + innerCircleSize);
invalidate();
}
public void setSide(int newSide)
{
if(newSide != Sides.LEFT && newSide != Sides.RIGHT && newSide != Sides.NOTHING)
{
return;
}
if(side == newSide)
{
return;
}
if(side == Sides.NOTHING)
{
side = newSide;
switch(newSide)
{
case Sides.LEFT:
innerCircleX = leftX;
break;
case Sides.RIGHT:
innerCircleX = rightX;
break;
}
moveSwitch(0, maxInnerCircleSize, animateDuration, null);
}
else if(newSide == Sides.LEFT)
{
side = newSide;
innerCircleX = rightX;
moveSwitch(innerCircleSize, 0, animateDuration, rightToLeftProxy);
}
else if(newSide == Sides.RIGHT)
{
side = newSide;
innerCircleX = leftX;
moveSwitch(innerCircleSize, 0, animateDuration, leftToRightProxy);
}
}
private void cancelCurrentAnimation()
{
if(currentAnimation != null)
{
currentAnimation.removeAllListeners();
currentAnimation.cancel();
currentAnimation = null;
}
}
public void moveSwitch(int start, int end, int duration, Animator.AnimatorListener listener)
{
cancelCurrentAnimation();
currentAnimation = new AnimatorSet();
currentAnimation.play(ObjectAnimator.ofInt(this, "innerCircleSize", start, end));
currentAnimation.setDuration(duration);
currentAnimation.setInterpolator(interpolator);
if(listener != null)
{
currentAnimation.addListener(listener);
}
currentAnimation.start();
}
public int getSide()
{
return side;
}
public void setLeftText(String lt)
{
leftText = lt == null ? "" : lt;
recalculate();
}
public void setRightText(String rt)
{
rightText = rt == null ? "" : rt;
recalculate();
}
public void setTextSize(int ts)
{
textSize = ts;
recalculate();
}
public void setCircleSize(int cs)
{
circleSize = cs;
recalculate();
}
private void recalculate()
{
circlePaint.setStrokeWidth(circleStrokeWidth);
leftTextWidth = (int)textPaint.measureText(leftText);
rightTextWidth = (int)textPaint.measureText(rightText);
textPaint.setTextSize(textSize);
Rect bounds = new Rect();
// textPaint.getTextBounds(leftText+rightText, 0, (leftText+rightText).length(), bounds);
textPaint.getTextBounds("1", 0, 1, bounds);
textHeight = bounds.height();
// Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
// textHeight = (int)(fontMetrics.bottom-fontMetrics.top);
// textHeight = (int)(fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading);
// Log.e(getClass().getName(), "textHeight " + textHeight + " textSize " + textSize + " fontMetrics " + (fontMetrics.bottom-fontMetrics.top));
float tmp = circleSize;
tmp /= 100;
innerCircleSize = (int)(tmp*80);
maxInnerCircleSize = innerCircleSize;
leftX = circleSize + circleStrokeWidth;
leftTextX = leftX + circleSize*2;
leftWidth = leftTextX + leftTextWidth;
rightX = leftWidth + circleSize*2 + circleStrokeWidth;
rightTextX = rightX + circleSize*2;
if(side == Sides.LEFT)
{
innerCircleX = leftX;
}
else if(side == Sides.RIGHT)
{
innerCircleX = rightX;
}
textPaint.setColor(textColor);
circlePaint.setColor(circleColor);
innerCirclePaint.setColor(innerCircleColor);
}
public void setListener(ChangeSideListener l)
{
listener = l;
}
public void setTextColor(int tc)
{
textColor = tc;
recalculate();
}
public void setCircleColor(int cc)
{
circleColor = cc;
recalculate();
}
public void setInnerCircleColor(int cc)
{
innerCircleColor = cc;
recalculate();
}
public void setInterpolator(Interpolator i)
{
if(i != null)
{
interpolator = i;
recalculate();
}
}
public interface ChangeSideListener
{
void changeSide(int newSide);
}
public interface Sides
{
int LEFT = 0;
int RIGHT = 1;
int NOTHING = -1;
}
}