package com.bawei.com.day_5switchbuttom.views;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.bawei.com.day_5switchbuttom.R;
/**
* Created by lenovo on 2017/12/28.
*/
/**
* SwitchButtom
* 点击和滑动,进行开关的控制
* 1.将两张图片初始化
* 2.绘制到View上面
* 3.处理事件
*/
public class Myviews extends View {
private Bitmap background;
private Bitmap slide;
private boolean state;
private int lastX;//处理事件
private OnStateChangeListener onStateChangeListener;监听对象
//new的时候调用
public Myviews(Context context) {
this(context, null);
}
//使用style的使用调用
public Myviews(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
//写在布局文件里面的时候调用
public Myviews(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);
canvas.drawBitmap(background,0,0,null);
int left = 0;
if (isTouch) {
//滑块和手指一起移动
// left = lastX;//修正位置
left = lastX - 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());
}
//给外部提供一个设置开关的方法
public void setState(boolean state) {
this.state = state;
}
public boolean getState(){
return state;
};
private boolean isTouch;//判断是否在触摸状态
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
isTouch=true;
lastX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
lastX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isTouch=false;
lastX = (int) event.getX();
//当手指抬起的时候,应该判断一下滑块儿的位置,然后设置开关的状态
// currentX 在中间位置的那边
int centerX = background.getWidth()/2;
if (lastX>centerX){
state = true;
}else {
state = false;
}
//调用接口里的方法
onStateChangeListener.OnStateChangeListener(state);
break;
}
//重新绘制
invalidate();
return true;
}
//接口回调
public interface OnStateChangeListener{
void OnStateChangeListener(boolean state);
}
//提供一个外部访问的方法
public void setOnStateChange(OnStateChangeListener onStateChangeListener){
this.onStateChangeListener = onStateChangeListener;
}
}
switchbuttom 滑动块
最新推荐文章于 2021-09-11 22:56:33 发布