<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="switchView_attrs">
<attr name="background" format="reference"></attr>
<attr name="slide" format="reference"></attr>
</declare-styleable>
</resources>
public SwitchView(Context context, AttributeSet attrs) {
super(context, attrs);
//拿到自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.switchView_attrs);
//拿到自定义字段的值
Drawable switchBackground = ta.getDrawable(R.styleable.switchView_attrs_background);
Drawable switchView_slide = ta.getDrawable(R.styleable.switchView_attrs_slide);
//把值设置到相应组件上
backgroundBitmap = convertDrawable2BitmapSimple(switchBackground);
switchSlide = convertDrawable2BitmapSimple(switchView_slide);
}
//将Drawable转成Bitmap
public Bitmap convertDrawable2BitmapSimple(Drawable drawable) {
BitmapDrawable bd= (BitmapDrawable)drawable;
return bd.getBitmap();
}
//控制控件的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (backgroundBitmap != null) {
//控件大小设置为背景的大小
setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
}else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
/**
* switchView开关监听接口
*
* */
interface OnSwitchChangedListener {
public void onSwitchChange(boolean isOpen);
}
/**
* 设置 switchView状态监听
* */
public void setOnChangeListener(OnSwitchChangedListener listener) {
switchListener = listener;
}
<com.example.custom.SwitchView
minguo:background="@drawable/switch_background"
minguo:slide="@drawable/slide_button_background"
android:id="@+id/switchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:minguo="http://schemas.android.com/apk/res/com.example.custom"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.custom.MainActivity" >
<com.example.custom.SwitchView
minguo:background="@drawable/switch_background"
minguo:slide="@drawable/slide_button_background"
android:id="@+id/switchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
/**
* 自定义开关按钮
* @author Administrator
*
*/
public class SwitchView extends View {
//背景图片和滑块图片
private Bitmap backgroundBitmap,switchSlide;
//画笔
private Paint paint;
//得到的x坐标(点击、移动、抬起)
private float currentX;
//判断开关是否打开的标记位
private boolean isOpen = false;
//开关打开与关闭的监听器
private OnSwitchChangedListener switchListener;
//滑块的四种状态
public static final int STATE_DOWN = 1; //按下的时候
public static final int STATE_MOVE = 2; //移动的时候
public static final int STATE_UP = 3; //抬起的时候
public static final int STATE_NONE = 0; //空的时候(即什么都没干的时候)
//标记状态(默认为空状态)
private int state = STATE_NONE;
public SwitchView(Context context) {
super(context,null);
}
public SwitchView(Context context, AttributeSet attrs) {
super(context, attrs);
//拿到自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.switchView_attrs);
//拿到自定义字段的值
Drawable switchBackground = ta.getDrawable(R.styleable.switchView_attrs_background);
Drawable switchView_slide = ta.getDrawable(R.styleable.switchView_attrs_slide);
//把值设置到相应组件上
backgroundBitmap = convertDrawable2BitmapSimple(switchBackground);
switchSlide = convertDrawable2BitmapSimple(switchView_slide);
}
//将Drawable转成Bitmap
public Bitmap convertDrawable2BitmapSimple(Drawable drawable) {
BitmapDrawable bd = (BitmapDrawable)drawable;
return bd.getBitmap();
}
//控制控件的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (backgroundBitmap != null) {
//控件大小设置为背景的大小
setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
}else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
//绘制控件的样式
@Override
protected void onDraw(Canvas canvas) {
//背景为空的时候才将背景绘制
if (backgroundBitmap != null) {
paint = new Paint();
//第一个参数表示需要画的Bitmap
//第二个参数表示Bitmap左边离控件的左边距
//第三个参数表示Bitmap上边离控件的上边距
//第四个参数表示画笔
canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
}
switch (state) {
case STATE_DOWN: //按下和移动的触发事件都一样,都是将滑块移动
case STATE_MOVE:
//当按下或移动的坐标大于滑块宽度一半时将滑块右移
if (currentX > switchSlide.getWidth()/2f) {
//让滑块向右滑动(重新绘制滑块的位置)
float left = currentX - switchSlide.getWidth()/2f;
//防止滑块移至背景外面,最大是滑块右边和背景右边对齐(即最大离左边为背景宽度-滑块宽度)
float maxLeft = backgroundBitmap.getWidth() - switchSlide.getWidth();
if (left > maxLeft) {
left = maxLeft;
}
canvas.drawBitmap(switchSlide, left, 0, paint);
//当按下或移动的坐标小于滑块宽度一半时滑块不动
}else if (currentX < switchSlide.getWidth()/2f) {
//让滑块不动就可以了
canvas.drawBitmap(switchSlide, 0, 0, paint);
}
break;
case STATE_NONE: //空或抬起的时候将滑块至于左边或右边
case STATE_UP:
//如果是打开的将滑块移动至右边,并将打开状态传至监听器
if (isOpen) {
if (switchListener != null) {
switchListener.onSwitchChange(true);
}
canvas.drawBitmap(switchSlide,
backgroundBitmap.getWidth() - switchSlide.getWidth(), 0, paint);
//如果是关闭的将滑块至于左边,并将关闭状态传至监听器
}else {
if (switchListener != null) {
switchListener.onSwitchChange(false);
}
canvas.drawBitmap(switchSlide, 0, 0, paint);
}
break;
default:
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
currentX = event.getX();
//将标记位修改成按下的状态
state = STATE_DOWN;
//通知系统重新绘制界面
invalidate();//在主线程
// postInvalidate();//在子线程
break;
case MotionEvent.ACTION_MOVE:
currentX = event.getX();
//将标记位修改为移动状态
state = STATE_MOVE;
invalidate();
break;
case MotionEvent.ACTION_UP:
currentX = event.getX();
//将标记为修改为抬起状态
state = STATE_UP;
//抬起的坐标大于背景宽度一半的时候设为打开状态
if (currentX > backgroundBitmap.getWidth()/2f) {
//滑块在右边,开启
isOpen = true;
//抬起的坐标小于背景宽度坐标一 半的时候设为关闭状态
}else if (currentX < backgroundBitmap.getWidth()) {
//滑块在左边,关闭
isOpen = false;
}
invalidate();
break;
}
return true;
}
/**
* switchView开关监听接口
*
* */
interface OnSwitchChangedListener {
public void onSwitchChange(boolean isOpen);
}
/**
* 设置 switchView状态监听
* */
public void setOnChangeListener(OnSwitchChangedListener listener) {
switchListener = listener;
}
}
public class MainActivity extends Activity {
private SwitchView switchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchView = (SwitchView) findViewById(R.id.switchView);
switchView.setOnChangeListener(new OnSwitchChangedListener() {
@Override
public void onSwitchChange(boolean isOpen) {
if (isOpen) {
//打开开关的时候的逻辑
Toast.makeText(MainActivity.this, "开关打开了", Toast.LENGTH_LONG).show();
}else {
//关闭开关的时候的逻辑
Toast.makeText(MainActivity.this, "开关关闭了", Toast.LENGTH_LONG).show();
}
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有