package com.qing;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MySlipSwitch extends View implements OnTouchListener {
//开关开启时的背景,关闭时的背景,滑动按钮
private Bitmap switch_on_Bkg, switch_off_Bkg, slip_Btn;
private Rect on_Rect, off_Rect;
//是否正在滑动
private boolean isSlipping = false;
//当前开关状态,true为开启,false为关闭
private boolean isSwitchOn = false;
//手指按下时的水平坐标X,当前的水平坐标X
private float previousX, currentX;
//开关监听器
private OnSwitchListener onSwitchListener;
//是否设置了开关监听器
private boolean isSwitchListenerOn = false;
public MySlipSwitch(Context context) {
super(context);
init();
}
public MySlipSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setOnTouchListener(this);
}
protected void setImageResource(int switchOnBkg, int switchOffBkg, int slipBtn) {
switch_on_Bkg = BitmapFactory.decodeResource(getResources(), switchOnBkg);
switch_off_Bkg = BitmapFactory.decodeResource(getResources(), switchOffBkg);
slip_Btn = BitmapFactory.decodeResource(getResources(), slipBtn);
//右半边Rect,即滑动按钮在右半边时表示开关开启
on_Rect = new Rect(switch_off_Bkg.getWidth() - slip_Btn.getWidth(), 0, switch_off_Bkg.getWidth(), slip_Btn.getHeight());
//左半边Rect,即滑动按钮在左半边时表示开关关闭
off_Rect = new Rect(0, 0, slip_Btn.getWidth(), slip_Btn.getHeight());
}
protected void setSwitchState(boolean switchState) {
isSwitchOn = switchState;
}
protected boolean getSwitchState() {
return isSwitchOn;
}
protected void updateSwitchState(boolean switchState) {
isSwitchOn = switchState;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Matrix matrix = new Matrix();
Paint paint = new Paint();
//滑动按钮的左边坐标
float left_SlipBtn;
//手指滑动到左半边的时候表示开关为关闭状态,滑动到右半边的时候表示开关为开启状态
if(currentX < (switch_on_Bkg.getWidth() / 2)) {
canvas.drawBitmap(switch_off_Bkg, matrix, paint);
} else {
canvas.drawBitmap(switch_on_Bkg, matrix, paint);
}
//判断当前是否正在滑动
if(isSlipping) {
if(currentX > switch_on_Bkg.getWidth()) {
left_SlipBtn = switch_on_Bkg.getWidth() - slip_Btn.getWidth();
} else {
left_SlipBtn = currentX - slip_Btn.getWidth() / 2;
}
} else {
//根据当前的开关状态设置滑动按钮的位置
if(isSwitchOn) {
left_SlipBtn = on_Rect.left;
} else {
left_SlipBtn = off_Rect.left;
}
}
//对滑动按钮的位置进行异常判断
if(left_SlipBtn < 0) {
left_SlipBtn = 0;
} else if(left_SlipBtn > switch_on_Bkg.getWidth() - slip_Btn.getWidth()) {
left_SlipBtn = switch_on_Bkg.getWidth() - slip_Btn.getWidth();
}
canvas.drawBitmap(slip_Btn, left_SlipBtn, 0, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(switch_on_Bkg.getWidth(), switch_on_Bkg.getHeight());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()) {
//滑动
case MotionEvent.ACTION_MOVE:
currentX = event.getX();
break;
//按下
case MotionEvent.ACTION_DOWN:
if(event.getX() > switch_on_Bkg.getWidth() || event.getY() > switch_on_Bkg.getHeight()) {
return false;
}
isSlipping = true;
previousX = event.getX();
currentX = previousX;
break;
//松开
case MotionEvent.ACTION_UP:
isSlipping = false;
//松开前开关的状态
boolean previousSwitchState = isSwitchOn;
if(event.getX() >= (switch_on_Bkg.getWidth() / 2)) {
isSwitchOn = true;
} else {
isSwitchOn = false;
}
//如果设置了监听器,则调用此方法
if(isSwitchListenerOn && (previousSwitchState != isSwitchOn)) {
onSwitchListener.onSwitched(isSwitchOn);
}
break;
default:
break;
}
//重新绘制控件
invalidate();
return true;
}
public void setOnSwitchListener(OnSwitchListener listener) {
onSwitchListener = listener;
isSwitchListenerOn = true;
}
public interface OnSwitchListener {
abstract void onSwitched(boolean isSwitchOn);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
滑动开关按钮.zip (38个子文件)
滑动开关按钮
MySlipSwitch 0.2
MySlipSwitch 0.2
project.properties 360B
.classpath 364B
assets
src
com
qing
MySlipSwitch.java 5KB
MainActivity.java 2KB
res
drawable-mdpi
ic_launcher.png 3KB
drawable-ldpi
ic_launcher.png 2KB
values
strings.xml 186B
layout
main.xml 1KB
drawable-hdpi
bkg_switch.png 10KB
btn_slip.png 7KB
ic_launcher.png 4KB
bin
classes
com
qing
MainActivity$1.class 969B
MySlipSwitch$OnSwitchListener.class 236B
R$string.class 397B
R$layout.class 364B
R$id.class 407B
MySlipSwitch.class 4KB
R$attr.class 310B
R$drawable.class 443B
MainActivity.class 1KB
MainActivity$2.class 1KB
R.class 461B
resources.ap_ 21KB
res
drawable-mdpi
ic_launcher.png 2KB
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
bkg_switch.png 7KB
btn_slip.png 2KB
ic_launcher.png 4KB
MySlipSwitch.apk 27KB
MySlipSwitch 0.4.apk 27KB
classes.dex 7KB
MySlipSwitch 0.2.apk 26KB
proguard.cfg 1KB
.project 848B
AndroidManifest.xml 782B
gen
com
qing
R.java 917B
MySlipSwitch 0.2.rar 153KB
readme.zip 12KB
共 38 条
- 1
资源评论
yimeixiaolangzai
- 粉丝: 1573
- 资源: 549
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功