public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TooggleButtonCustom buttonCustom = (TooggleButtonCustom) findViewById(R.id.custom_button);
//设置背景图片
buttonCustom.setBackGroundPic(R.drawable.switch_background);
//设置前边的图片
buttonCustom.setForePic(R.drawable.slide_button_background);
//设置默认状态
buttonCustom.setState(false);
}
}
package com.example.togglebuttoncustomdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class TooggleButtonCustom extends View {
private Bitmap backBitmap;
private Bitmap foreBitmap;
private boolean state = false;
private int currentX;
boolean isTouching;
/**
* java代码中创建时使用这个构造方法
*
* @param context
*/
public TooggleButtonCustom(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* xml布局文件中使用
*
* @param context
* @param attrs
*/
public TooggleButtonCustom(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 有style资源文件时使用
*
* @param context
* @param attrs
* @param defStyle
*/
public TooggleButtonCustom(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
* 设置背景图片
*
* @param switchBackground
*/
public void setBackGroundPic(int switchBackground) {
backBitmap = BitmapFactory.decodeResource(getResources(),
switchBackground);
}
/**
* 设置前边图片
*
* @param slideButtonBackground
*/
public void setForePic(int slideButtonBackground) {
foreBitmap = BitmapFactory.decodeResource(getResources(),
slideButtonBackground);
}
/**
* 设置开关状态
*
* @param b
*/
public void setState(boolean b) {
this.state = b;
}
/**
* 测量控件的宽高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(backBitmap.getWidth(), backBitmap.getHeight());
}
/**
* 绘制控件
*/
@Override
protected void onDraw(Canvas canvas) {
// 1.平铺背景图片
canvas.drawBitmap(backBitmap, 0, 0, null);
if (isTouching) {
int left = currentX - foreBitmap.getWidth()/2;
//两个问题 ,位置不在中心,,移出范围
if (left < 0) {
left = 0;
}else if (left > backBitmap.getWidth() - foreBitmap.getWidth()) {
left = backBitmap.getWidth() - foreBitmap.getWidth();
}
canvas.drawBitmap(foreBitmap, left, 0, null);
}else {
// 2.根据状态去绘制 上面图片
if (state) {
// 右边
canvas.drawBitmap(foreBitmap,
backBitmap.getWidth() - foreBitmap.getWidth(), 0, null);
} else {
// 左边
canvas.drawBitmap(foreBitmap, 0, 0, null);
}
}
}
/**
* 触摸控件时调用的方法
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouching = true;
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
isTouching = true;
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isTouching = false;
currentX = (int) event.getX();
state = currentX > backBitmap.getWidth()/2;
break;
default:
break;
}
invalidate();//重复绘制控件 自动调用OnDraw()
return true;// 自己处理触摸事件
}
}