java有滑动按钮_Android滑动开关-ToggleButton(附源码)

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值