/**
* SwitchButtom
* 点击和滑动,进行开关的控制
* 1.将两张图片初始化
* 2.绘制到View上面
* 3.处理事件
*/
* SwitchButtom
* 点击和滑动,进行开关的控制
* 1.将两张图片初始化
* 2.绘制到View上面
* 3.处理事件
*/
public class SwitchButtom extends View {
private Bitmap background;
private Bitmap slide;
private boolean state;//开关的状态
private OnStateChangeListener onStateChangeListener;//监听对象
private Bitmap slide;
private boolean state;//开关的状态
private OnStateChangeListener onStateChangeListener;//监听对象
//new的时候调用
public SwitchButtom(Context context) {
this(context, null);
}
public SwitchButtom(Context context) {
this(context, null);
}
//使用style的使用调用
public SwitchButtom(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SwitchButtom(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
//写在布局文件里面的时候调用
public SwitchButtom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//初始化两张图片
//将资源 文件转成Bitmap对象
// getResources() 包含图片资源的资源对象
// R.drawable.background 图片资源的id
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
slide = BitmapFactory.decodeResource(getResources(), R.drawable.slide);
}
//将图片绘制到控件上面
public SwitchButtom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//初始化两张图片
//将资源 文件转成Bitmap对象
// getResources() 包含图片资源的资源对象
// R.drawable.background 图片资源的id
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
slide = BitmapFactory.decodeResource(getResources(), R.drawable.slide);
}
//将图片绘制到控件上面
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制
//参数1.图片对象
//参数2.x轴的开始位置
//参数3.y轴的开始位置
//参数4.画笔
canvas.drawBitmap(background, 0, 0, null);//背景
int left = 0;
if (isTouch) {
//滑块和手指一起移动
// left = currentX;//修正位置
left = currentX - slide.getWidth() / 2;
//处理边界问题
if (left < 0) {
left = 0;
}
if (left > background.getWidth() - slide.getWidth()) {
left = background.getWidth() - slide.getWidth();
}
canvas.drawBitmap(slide, left, 0, null);//滑块儿
} else {
//根据开关的状态绘制滑块
if (state) {
// getWidth()---背景的宽度
left = background.getWidth() - slide.getWidth();
} else {
left = 0;
}
canvas.drawBitmap(slide, left, 0, null);//滑块儿
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制
//参数1.图片对象
//参数2.x轴的开始位置
//参数3.y轴的开始位置
//参数4.画笔
canvas.drawBitmap(background, 0, 0, null);//背景
int left = 0;
if (isTouch) {
//滑块和手指一起移动
// left = currentX;//修正位置
left = currentX - slide.getWidth() / 2;
//处理边界问题
if (left < 0) {
left = 0;
}
if (left > background.getWidth() - slide.getWidth()) {
left = background.getWidth() - slide.getWidth();
}
canvas.drawBitmap(slide, left, 0, null);//滑块儿
} else {
//根据开关的状态绘制滑块
if (state) {
// getWidth()---背景的宽度
left = background.getWidth() - slide.getWidth();
} else {
left = 0;
}
canvas.drawBitmap(slide, left, 0, null);//滑块儿
}
}
//将控件的尺寸重新设置一下
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//根据背景图设置控件的宽高
setMeasuredDimension(background.getWidth(), background.getHeight());//将需要的宽高设置进去
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//根据背景图设置控件的宽高
setMeasuredDimension(background.getWidth(), background.getHeight());//将需要的宽高设置进去
}
//给外部提供一个设置开关的方法
public void setState(boolean state) {
this.state = state;
}
public boolean getState(){
return state;
};
//处理事件
private int currentX;
private boolean isTouch;//判断是否在触摸状态
public void setState(boolean state) {
this.state = state;
}
public boolean getState(){
return state;
};
//处理事件
private int currentX;
private boolean isTouch;//判断是否在触摸状态
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouch = true;
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isTouch = false;
currentX = (int) event.getX();
//当手指抬起的时候,应该判断一下滑块儿的位置,然后设置开关的状态
// currentX 在中间位置的那边
int centerX = background.getWidth()/2;
if (currentX>centerX){
state = true;
}else {
state = false;
}
//调用接口里的方法
onStateChangeListener.onStateChange(state);
break;
}
//重新绘制
invalidate();
return true;
}
//接口回调
public interface OnStateChangeListener{
void onStateChange(boolean state);
}
//提供一个外部访问的方法
public void setOnStateChange(OnStateChangeListener onStateChangeListener){
this.onStateChangeListener = onStateChangeListener;
}
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouch = true;
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isTouch = false;
currentX = (int) event.getX();
//当手指抬起的时候,应该判断一下滑块儿的位置,然后设置开关的状态
// currentX 在中间位置的那边
int centerX = background.getWidth()/2;
if (currentX>centerX){
state = true;
}else {
state = false;
}
//调用接口里的方法
onStateChangeListener.onStateChange(state);
break;
}
//重新绘制
invalidate();
return true;
}
//接口回调
public interface OnStateChangeListener{
void onStateChange(boolean state);
}
//提供一个外部访问的方法
public void setOnStateChange(OnStateChangeListener onStateChangeListener){
this.onStateChangeListener = onStateChangeListener;
}
}
//Main
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchButtom myButtom = (SwitchButtom) findViewById(R.id.mybutton);
//在外部设置监听
myButtom.setOnStateChange(new SwitchButtom.OnStateChangeListener() {
@Override
public void onStateChange(boolean state) {
Toast.makeText(MainActivity.this,"当前状态---"+state,Toast.LENGTH_SHORT).show();
}
});
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchButtom myButtom = (SwitchButtom) findViewById(R.id.mybutton);
//在外部设置监听
myButtom.setOnStateChange(new SwitchButtom.OnStateChangeListener() {
@Override
public void onStateChange(boolean state) {
Toast.makeText(MainActivity.this,"当前状态---"+state,Toast.LENGTH_SHORT).show();
}
});
}
}
//main布局还是用LinearLayout
<com.gjl.day04_switchbuttom.views.SwitchButtom
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>