-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathSlideSwitch.java
More file actions
305 lines (280 loc) · 8.38 KB
/
SlideSwitch.java
File metadata and controls
305 lines (280 loc) · 8.38 KB
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
/*
* Copyright (C) 2015 Quinn Chen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.leaking.slideswitch;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
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.graphics.RectF;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import com.example.slideswitch.R;
public class SlideSwitch extends View {
public static final int SHAPE_RECT = 1;
public static final int SHAPE_CIRCLE = 2;
private static final int RIM_SIZE = 6;
private static final int DEFAULT_COLOR_THEME = Color.parseColor("#ff00ee00");
// 3 attributes
private int color_theme;
private boolean isOpen;
private int shape;
// varials of drawing
private Paint paint;
private Rect backRect;
private Rect frontRect;
private RectF frontCircleRect;
private RectF backCircleRect;
private int alpha;
private int max_left;
private int min_left;
private int frontRect_left;
private int frontRect_left_begin = RIM_SIZE;
private int eventStartX;
private int eventLastX;
private int diffX = 0;
private boolean slideable = true;
private SlideListener listener;
public interface SlideListener {
public void open();
public void close();
}
public SlideSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
listener = null;
paint = new Paint();
paint.setAntiAlias(true);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.slideswitch);
color_theme = a.getColor(R.styleable.slideswitch_themeColor,
DEFAULT_COLOR_THEME);
isOpen = a.getBoolean(R.styleable.slideswitch_isOpen, false);
shape = a.getInt(R.styleable.slideswitch_shape, SHAPE_RECT);
a.recycle();
}
public SlideSwitch(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideSwitch(Context context) {
this(context, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = measureDimension(280, widthMeasureSpec);
int height = measureDimension(140, heightMeasureSpec);
if (shape == SHAPE_CIRCLE) {
if (width < height)
width = height * 2;
}
setMeasuredDimension(width, height);
initDrawingVal();
}
public void initDrawingVal() {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
backCircleRect = new RectF();
frontCircleRect = new RectF();
frontRect = new Rect();
backRect = new Rect(0, 0, width, height);
min_left = RIM_SIZE;
if (shape == SHAPE_RECT)
max_left = width / 2;
else
max_left = width - (height - 2 * RIM_SIZE) - RIM_SIZE;
if (isOpen) {
frontRect_left = max_left;
alpha = 255;
} else {
frontRect_left = RIM_SIZE;
alpha = 0;
}
frontRect_left_begin = frontRect_left;
}
public int measureDimension(int defaultSize, int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = defaultSize; // UNSPECIFIED
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
if (shape == SHAPE_RECT) {
paint.setColor(Color.GRAY);
canvas.drawRect(backRect, paint);
paint.setColor(color_theme);
paint.setAlpha(alpha);
canvas.drawRect(backRect, paint);
frontRect.set(frontRect_left, RIM_SIZE, frontRect_left
+ getMeasuredWidth() / 2 - RIM_SIZE, getMeasuredHeight()
- RIM_SIZE);
paint.setColor(Color.WHITE);
canvas.drawRect(frontRect, paint);
} else {
// draw circle
int radius;
radius = backRect.height() / 2 - RIM_SIZE;
paint.setColor(Color.GRAY);
backCircleRect.set(backRect);
canvas.drawRoundRect(backCircleRect, radius, radius, paint);
paint.setColor(color_theme);
paint.setAlpha(alpha);
canvas.drawRoundRect(backCircleRect, radius, radius, paint);
frontRect.set(frontRect_left, RIM_SIZE, frontRect_left
+ backRect.height() - 2 * RIM_SIZE, backRect.height()
- RIM_SIZE);
frontCircleRect.set(frontRect);
paint.setColor(Color.WHITE);
canvas.drawRoundRect(frontCircleRect, radius, radius, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (slideable == false)
return super.onTouchEvent(event);
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
eventStartX = (int) event.getRawX();
break;
case MotionEvent.ACTION_MOVE:
eventLastX = (int) event.getRawX();
diffX = eventLastX - eventStartX;
int tempX = diffX + frontRect_left_begin;
tempX = (tempX > max_left ? max_left : tempX);
tempX = (tempX < min_left ? min_left : tempX);
if (tempX >= min_left && tempX <= max_left) {
frontRect_left = tempX;
alpha = (int) (255 * (float) tempX / (float) max_left);
invalidateView();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
stopToggle(event);
break;
default:
break;
}
return true;
}
private void stopToggle(MotionEvent event) {
int wholeX = (int) (event.getRawX() - eventStartX);
frontRect_left_begin = frontRect_left;
boolean toRight;
toRight = (frontRect_left_begin > max_left / 2);
if (Math.abs(wholeX) < 3) {
toRight = !toRight;
}
moveToDest(toRight);
}
/**
* draw again
*/
private void invalidateView() {
if (Looper.getMainLooper() == Looper.myLooper()) {
invalidate();
} else {
postInvalidate();
}
}
public void setSlideListener(SlideListener listener) {
this.listener = listener;
}
public void moveToDest(final boolean toRight) {
ValueAnimator toDestAnim = ValueAnimator.ofInt(frontRect_left,
toRight ? max_left : min_left);
toDestAnim.setDuration(500);
toDestAnim.setInterpolator(new AccelerateDecelerateInterpolator());
toDestAnim.start();
toDestAnim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
frontRect_left = (Integer) animation.getAnimatedValue();
alpha = (int) (255 * (float) frontRect_left / (float) max_left);
invalidateView();
}
});
toDestAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (toRight) {
isOpen = true;
if (listener != null)
listener.open();
frontRect_left_begin = max_left;
} else {
isOpen = false;
if (listener != null)
listener.close();
frontRect_left_begin = min_left;
}
}
});
}
public void setState(boolean isOpen) {
this.isOpen = isOpen;
initDrawingVal();
invalidateView();
if (listener != null)
if (isOpen == true) {
listener.open();
} else {
listener.close();
}
}
public void setShapeType(int shapeType) {
this.shape = shapeType;
}
public void setSlideable(boolean slideable) {
this.slideable = slideable;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
this.isOpen = bundle.getBoolean("isOpen");
state = bundle.getParcelable("instanceState");
}
super.onRestoreInstanceState(state);
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putBoolean("isOpen", this.isOpen);
return bundle;
}
}