滑块按钮
滑塊按鈕的實現(xiàn)
運(yùn)行的效果:
用到的圖片:(放置到drawable_xhdpi目錄下)
? ? ? ? ? ?
public class SlipButton extends View{?
??
??? private Bitmap bitmapDrawUnderSlid;
??? private Bitmap bitmapDrawSlid;
??? private Paint paint;
??? private int backWidth;????????????????? //滑座圖片的寬度
??? private int slidWidth;????????????????? //滑塊圖片的寬度
??? private float location;???????????????? //手觸摸的位置
??? private boolean isOn = true;???????????//是打開還是關(guān)閉
??? private boolean isSliding = false;????? //是否正在滑動
???
??? public SlipButton(Context context,AttributeSetattrs) {
?????? super(context);?
?????? init();
??? }??
???
??? /**
??? ?* 初始化參數(shù)
??? ?*/
??? public void init(){
?????? bitmapDrawUnderSlid = BitmapFactory.decodeResource(getResources(),R.drawable.slid_button_background);
?????? bitmapDrawSlid = BitmapFactory.decodeResource(getResources(),R.drawable.slid_button);
?????? backWidth = bitmapDrawUnderSlid.getWidth();
?????? slidWidth = bitmapDrawSlid.getWidth();
?????? paint = new Paint();
??? }
???
??? /**
??? ?* 重寫view的onDraw函數(shù)
??? ?*/
??? @Override
??? protected void onDraw(Canvas canvas) {
?????? if(isSliding){
??????????
?????? }else{
?????????? if(location > backWidth/2 - slidWidth/2){
????????????? location = backWidth/2;
?????????? }else{
????????????? location = 0;
?????????? }
?????? }
?????? canvas.drawBitmap(bitmapDrawUnderSlid, 0, 0, paint);
?????? canvas.drawBitmap(bitmapDrawSlid,location, 0, paint);??
??? }
???
??? /**
??? ?* 控制滑塊的移動,并且會重新設(shè)置移動的位置的參數(shù)
??? ?*/
??? @Override
??? public boolean onTouchEvent(MotionEventevent) {?
?????? switch (event.getAction()) {
?????????? case MotionEvent.ACTION_DOWN:{?????????????????? //手按下的時候
????????????? location = event.getX();
????????????? isSliding = true;
????????????? if(location > backWidth - slidWidth){
????????????????? location = backWidth - slidWidth;
????????????????? isOn = false;
????????????? }else if(location < slidWidth/2){
????????????????? location = 0;
????????????????? isOn = true;
????????????? }else{
????????????????? location=location - slidWidth/2;
????????????? }
????????????? break;
?????????? }
??????
?????????? case MotionEvent.ACTION_UP:{????????????//手指離開的時候
????????????? location = event.getX();
????????????? isSliding = false;
????????????? if(location > backWidth - slidWidth){
????????????????? location = backWidth - slidWidth;
????????????????? isOn = false;
????????????? }else if(location < slidWidth/2){
????????????????? location = 0;
????????????????? isOn = true;
????????????? }else{
????????????????? location=location - slidWidth/2;
????????????? }
????????????? break;
?????????? }??
?????????????
?????????? case MotionEvent.ACTION_MOVE:{?????????//手指移動的時候
????????????? isSliding = true;
????????????? location = event.getX();
????????????? if(location > backWidth - slidWidth){
????????????????? location = backWidth - slidWidth;
????????????????? isOn = false;
????????????? }else if(location < slidWidth/2){
????????????????? isOn = true;
????????????????? location = 0;
????????????? }else{
????????????????? location=location - slidWidth/2;
????????????? }
????????????? break;
?????????? }
??????????
?????????? default:break;
?????? }??
?????? invalidate();???????? //此函數(shù)是view自帶的函數(shù),用于調(diào)用onDraw函數(shù)重新繪制
?????? return true;
??? }??
}
總結(jié)
- 上一篇: Android listView 去掉h
- 下一篇: Android activity之间的滑