MainActivity 调用滑动开关, 初始化并设置监听
public class MainActivity extends Activity {
private ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.togglebutton);
toggleButton.setSlideBackgroundResource(R.drawable.slide_button_background);
toggleButton.setSwitchBackgroundResource(R.drawable.switch_background);
toggleButton.setToggleState(ToggleState.Close);
toggleButton.setOnToggleStateChangeListener(new OnToggleStateChangeListener(){
@Override
public void onToggleStateChange(ToggleState state) {
Toast.makeText(MainActivity.this, state==ToggleState.Open?"开启":"关闭", 0).show();
}
});
}
}
自定义滑动开关View
public class ToggleButton extends View {
//开关的状态
private ToggleState toggleState = ToggleState.Open;
private Bitmap slideBg;
private Bitmap switchBg;
private int currentX; //当前触摸点x坐标
private boolean isSliding = false;
/**
* 如果你的view需要在java代码中动态new出来,就使用这个构造方法
* @param context
*/
public ToggleButton(Context context) {
super(context);
}
public enum ToggleState{
Open,Close
}
/**
* 如果你的view只是在布局文件中使用, 只需要重写这个构造
* @param context
* @param attrs
*/
public ToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置滑动块的背景图片
* @param slideButtonBackground
*/
public void setSlideBackgroundResource(int slideButtonBackground) {
slideBg = BitmapFactory.decodeResource(getResources(), slideButtonBackground);
}
/**
* 设置滑块的图片
* @param switchBackground
*/
public void setSwitchBackgroundResource(int switchBackground) {
switchBg = BitmapFactory.decodeResource(getResources(), switchBackground);
}
/**
* 设置开关的状态
* @param state
*/
public void setToggleState(ToggleState state) {
toggleState = state;
}
/**
* 设置当前控件显示在屏幕上的宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(switchBg.getWidth(), switchBg.getHeight());
}
/**
* 绘制自己在屏幕上的样子
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//1.绘制背景图片
//left图片的左边的x坐标
//top图片的顶部y坐标
canvas.drawBitmap(switchBg, 0, 0, null);
//2.绘制滑块的图片
if(isSliding){
int left = currentX -slideBg.getWidth()/2;
if(left<0){
left=0;
}
if((left+slideBg.getWidth())>switchBg.getWidth()){
left = switchBg.getWidth()-slideBg.getWidth();
}
canvas.drawBitmap(slideBg, left, 6, null);
}else{
//此时抬起, 根据state去绘制滑动块位置
if(toggleState==ToggleState.Open){
canvas.drawBitmap(slideBg, switchBg.getWidth()-slideBg.getWidth(), 6, null);
}else{
canvas.drawBitmap(slideBg, 0, 6, null);
}
}
//
}
@Override
public boolean onTouchEvent(MotionEvent event) {
currentX = (int) event.getX();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
isSliding = true;
break;
case MotionEvent.ACTION_UP:
isSliding = false;
int centerX = switchBg.getWidth()/2;
if(currentX>centerX){
//open
if(toggleState!=ToggleState.Open){
toggleState = ToggleState.Open;
if(listener!=null){
listener.onToggleStateChange(toggleState);
}
}
}else{
//close
if(toggleState!=ToggleState.Close){
toggleState = ToggleState.Close;
if(listener!=null){
listener.onToggleStateChange(toggleState);
}
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
}
//调用ondraw方法
invalidate();
return true;
}
private OnToggleStateChangeListener listener;
public void setOnToggleStateChangeListener(OnToggleStateChangeListener listener){
this.listener = listener;
}
public interface OnToggleStateChangeListener{
void onToggleStateChange(ToggleState state);
}
}