Android 自定义toggleButton

本文介绍了一个自定义的切换按钮组件实现细节,包括如何通过触摸事件改变按钮状态,并提供了对外暴露的状态变化监听接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MyToggleButton extends View {

private Bitmap switchBackgroundBm;
private Bitmap slideButtonBackgroundBm;
private boolean state;// 滑动块的开关状态
private int currentX;
private boolean isTouch;
private OnMyToggleButtonStateChanged listener;
// 在代码中new对象时,调用
public MyToggleButton(Context context) {
super(context);
}
// 使用样式时,调用
public MyToggleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// 在布局文件中使用控件时,调用
public MyToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
// 获取控件身上的属性
String namespace = "http://schemas.android.com/apk/res-auto";
int slideButtonBackgroundRes = attrs.getAttributeResourceValue(namespace, "slideButtonBackground", -1);
setSlideButtonRes(slideButtonBackgroundRes);


int switchBackgroundRes = attrs.getAttributeResourceValue(namespace, "switchBackground", -1);
setBackgroundRes(switchBackgroundRes);

boolean slideState = attrs.getAttributeBooleanValue(namespace, "slideState", false);
setSlideState(slideState);

}
// 设置背景图片
public void setBackgroundRes(int switchBackground) {
switchBackgroundBm = BitmapFactory.decodeResource(getResources(), switchBackground);
}
// 设置滑动块
public void setSlideButtonRes(int slideButtonBackground) {
slideButtonBackgroundBm = BitmapFactory.decodeResource(getResources(), slideButtonBackground);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// 测量自己的宽高,为背景图片的宽高
setMeasuredDimension(switchBackgroundBm.getWidth(), switchBackgroundBm.getHeight());
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制背景
canvas.drawBitmap(switchBackgroundBm, 0, 0, null);

// 根据手指的移动,绘制滑动块
if(isTouch){
float left = currentX - slideButtonBackgroundBm.getWidth()/2;// 让滑动块的中心随手指移动
// 判断边界
if(left<0){// 左边界
left = 0;
}else if(left>switchBackgroundBm.getWidth()-slideButtonBackgroundBm.getWidth()){// 右边界
left = switchBackgroundBm.getWidth()-slideButtonBackgroundBm.getWidth();
}
// 绘制滑动块
canvas.drawBitmap(slideButtonBackgroundBm, left, 0, null);
}else{
float left = 0;
if(state){
left = switchBackgroundBm.getWidth()-slideButtonBackgroundBm.getWidth();
}else{
left = 0;
}
// 绘制滑动块
canvas.drawBitmap(slideButtonBackgroundBm, left, 0, null);
}
}
public void setSlideState(boolean state) {
this.state = state;
}
// 处理触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
currentX = (int) event.getX();
isTouch = true;
break;
case MotionEvent.ACTION_MOVE:
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
currentX = (int) event.getX();
isTouch = false;
// 手指抬起时,根据滑动块的中心和背景的中心比较
//  如果滑动块的中心>背景的中心,把状态变为开
// 如果滑动块的中心<背景的中心,把状态变为关
int center = switchBackgroundBm.getWidth()/2;
boolean tempState = currentX>center;
// 当状态发生变化时,调用外界的业务
if(tempState!=state&&listener!=null){
listener.onStateChanged(tempState);
}
state = tempState;
break;


default:
break;
}
// 触发onDraw方法
invalidate();
return true;// 消费掉事件,为了接受到move、up事件
}

// 对外暴露接口
public interface OnMyToggleButtonStateChanged{
void onStateChanged(boolean state);
}

// 让外界把接口传递进来
public void setOnMyToggleButtonStateChanged(OnMyToggleButtonStateChanged listener){
this.listener = listener;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值