SwitchButtonView.java
6.94 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
package com.sw.laryngoscope.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.sw.laryngoscope.R;
/**
* Created by Administrator on 3/22 0022.
*/
public class SwitchButtonView extends View {
private Paint mPaint;
//背景
private Bitmap bitmapBackGround;
//小球
private Bitmap bitmapBall;
//底部
private Bitmap bitmapBottom;
//黑色
private Bitmap bitmapBlack;
//取重叠部分
private PorterDuffXfermode pdf;
//开关状态
private boolean isChecked;
//触摸X坐标
private int touchX;
//小球X坐标
private int ballX = 0;
//小球运动状态
private int ballMoveState = LEFT_MOST;
//图层标识
private int saveFlags;
//switch的宽度
private int switchWidth;
//switch的高度
private int switchHeight;
//最左边
private static final int LEFT_MOST = 0;
//最右边
private static final int RIGHT_MOST = 1;
//手指按下
private static final int TOUCH_STATE_DOWN = 2;
//手指移动
private static final int TOUCH_STATE_MOVE = 3;
//手指抬起
private static final int TOUCH_STATE_UP = 4;
private onSwitchListener mListener;
public SwitchButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SwitchButtonView);
isChecked = ta.getBoolean(R.styleable.SwitchButtonView_checked, false);
ta.recycle();
init(context);
}
public SwitchButtonView(Context context) {
this(context, null);
}
private void init(Context context) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
pdf = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); //2张重叠 取上面一张重叠部分
/*saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG |
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;*/
/*bitmapBackGround = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_bg);
bitmapBall = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_ball);
bitmapBottom = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_bottom);
bitmapBlack = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_bottom);*/
switchWidth = bitmapBackGround.getWidth();
switchHeight = bitmapBackGround.getHeight();
//开
if (isChecked) {
ballMoveState = RIGHT_MOST;
ballX = bitmapBackGround.getWidth() - bitmapBall.getWidth();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(switchWidth, switchHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//增加图层
canvas.saveLayer(0, 0, switchWidth, switchHeight, null, saveFlags);
//底部是黑色图层
canvas.drawBitmap(bitmapBlack, 0, 0, mPaint);
mPaint.setXfermode(pdf);
if (isChecked) {
canvas.drawBitmap(bitmapBottom, 0 - (switchWidth - bitmapBall.getWidth() - ballX), 0, mPaint);
} else {
canvas.drawBitmap(bitmapBottom, -(bitmapBottom.getWidth() / 2 - bitmapBall.getWidth() / 2) + ballX, 0, mPaint);
}
mPaint.setXfermode(null);
canvas.restore();
ballMoveState(canvas);
}
/**
* 滑动状态绘制
*
* @param canvas
*/
private void ballMoveState(Canvas canvas) {
switch (ballMoveState) {
case TOUCH_STATE_DOWN:
case TOUCH_STATE_MOVE:
if (touchX > 0 && touchX < switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall, touchX, 0, mPaint);
} else if (touchX <= 0) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall, switchWidth - bitmapBall.getWidth(), 0, mPaint);
}
break;
case TOUCH_STATE_UP:
case LEFT_MOST:
if (touchX > 0 && touchX < switchWidth / 2) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth / 2 && touchX <= switchWidth) {
canvas.drawBitmap(bitmapBall, switchWidth - bitmapBall.getWidth(), 0, mPaint);
} else if (touchX <= 0) {
canvas.drawBitmap(bitmapBall, 0, 0, mPaint);
} else if (touchX >= switchWidth - bitmapBall.getWidth()) {
canvas.drawBitmap(bitmapBall, switchWidth - bitmapBall.getWidth(), 0, mPaint);
}
break;
case RIGHT_MOST:
canvas.drawBitmap(bitmapBall, switchWidth - bitmapBall.getWidth(), 0, mPaint);
break;
default:
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStateChange((int) event.getX(), TOUCH_STATE_DOWN);
break;
case MotionEvent.ACTION_MOVE:
touchStateChange((int) event.getX(), TOUCH_STATE_MOVE);
break;
case MotionEvent.ACTION_UP:
touchStateChange((int) event.getX(), TOUCH_STATE_UP);
break;
default:
break;
}
return true;
}
/**
* 触摸状态改变
*
* @param mTouchX
* @param touchState
*/
private void touchStateChange(int mTouchX, int touchState) {
ballX = touchX = mTouchX;
if (touchX <= 0) {
ballX = 0;
}
if (touchX >= switchWidth - bitmapBall.getWidth()) {
ballX = switchWidth - bitmapBall.getWidth();
}
ballMoveState = touchState;
if (ballMoveState == TOUCH_STATE_UP) { //手指抬起
ballX = 0;
if (touchX >= switchWidth / 2f) {
isChecked = true;
ballX = switchWidth - bitmapBall.getWidth();
} else {
isChecked = false;
}
if (mListener != null) {
mListener.onSwitchChanged(isChecked);
}
}
invalidate();
}
public void setOnSwitchListener(onSwitchListener listener) {
this.mListener = listener;
}
public interface onSwitchListener {
void onSwitchChanged(boolean isCheck);
}
}