packagecom.fay.toggle;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Matrix;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.MotionEvent;importandroid.view.View;/*** toggle the status
*@since2014/05/22
*@authorFay
* {@link1940125001@qq.com}*/
public class ToggleButton extendsView {private String TAG = "ToggleButton";//the bitmap of toggle on
private Bitmap backgroudBitmap = null;//the bitmap of toggle flip
private Bitmap slidingBitmap = null;//whether is button if is Sliding
private boolean isSliding = false;//the previous state of the button
private boolean previousState = false;private Paint mPaint = newPaint();private Matrix mMatrix = newMatrix();private OnToggleStateChangedListener mOnToggleStateChangedListener = null;//current X-Location which touched
private float touchXLocation = 0;//the slidingBitmap inner margin the ToggleButton
private float marginLeft = 0;publicToggleButton(Context context) {super(context);
}publicToggleButton(Context context, AttributeSet attrs) {super(context, attrs);
}public ToggleButton(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);
}/*** set the background for the ToggleButton and sliding image resource
*@paramint backgroudResID
*@paramint flipResID*/
public void setImageResource(int backgroudResID, intflipResID) {
backgroudBitmap=BitmapFactory.decodeResource(getResources(), backgroudResID);
slidingBitmap=BitmapFactory.decodeResource(getResources(), flipResID);
}/*** set the initialize state of the view
*@paramboolean isOn*/
public void setInitState(booleanisOn) {
previousState=isOn;
}
@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {//TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Overrideprotected voidonDraw(Canvas canvas) {
canvas.drawBitmap(backgroudBitmap, mMatrix, mPaint);if (isSliding) {//if sliding//to avoid slidingBitmap sliding out of the ToggleButton
if (touchXLocation >= backgroudBitmap.getWidth() - slidingBitmap.getWidth() /2
|| touchXLocation <= slidingBitmap.getWidth() /2) {if (touchXLocation >= backgroudBitmap.getWidth() - slidingBitmap.getWidth() /2) {
marginLeft= backgroudBitmap.getWidth() -slidingBitmap.getWidth();
}else{
marginLeft= 0;
}
}else{
marginLeft= touchXLocation - slidingBitmap.getWidth() / 2;
}
canvas.drawBitmap(slidingBitmap, marginLeft,0, mPaint);
}else{if (previousState == true) {//on
canvas.drawBitmap(slidingBitmap, backgroudBitmap.getWidth() - slidingBitmap.getWidth(), 0, mPaint);
}else{
canvas.drawBitmap(slidingBitmap,0, 0, mPaint);
}
}super.onDraw(canvas);
}
@Overridepublic booleanonTouchEvent(MotionEvent event) {switch(event.getAction()) {caseMotionEvent.ACTION_DOWN:if (0 <= event.getX() && event.getX() <=backgroudBitmap.getWidth()&& 0 <= event.getY() && event.getY() <=backgroudBitmap.getHeight() ) {
touchXLocation=event.getX();
isSliding= true;
}else{
isSliding= false;
}break;caseMotionEvent.ACTION_MOVE:if (isSliding) {//to avoid change the state out of the toggle
touchXLocation =event.getX();
}break;caseMotionEvent.ACTION_UP:
isSliding= false;if (touchXLocation > backgroudBitmap.getWidth() / 2) {//on//if previous state is off
if (previousState == false) {
mOnToggleStateChangedListener.changed(true);
previousState= true;
}
}else if (touchXLocation < backgroudBitmap.getWidth() / 2) {//off//if previous state if on
if (previousState == true) {
mOnToggleStateChangedListener.changed(false);
previousState= false;
}
}break;
}
invalidate();return true;
}/*** The Listener of this ToggleButton*/
public interfaceOnToggleStateChangedListener {void changed(booleanisOn);
}/*** set the Listener for the ToggleButton*/
public voidsetOnStateChangedListener(OnToggleStateChangedListener mOnToggleStateChangedListener) {this.mOnToggleStateChangedListener =mOnToggleStateChangedListener;
}
}