android 自訂滑動按鈕

來源:互聯網
上載者:User

標籤:

  第一接觸公司項目就讓我畫頁面,而且還涉及到我最討厭的自訂view  但是沒辦法,討厭也必須要做啊,經過百度上資源的尋找,終於寫出了一個滑動控制項。廢話不多說,上代碼。

 

 

 

package com.etong.cpms.widget;

import com.etong.cpms.activity.R;

import android.content.Context;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.graphics.Canvas;  
import android.graphics.Color;
import android.graphics.Matrix;  
import android.graphics.Paint;  
import android.util.AttributeSet;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.View.OnTouchListener;

public class WiperSwitch extends View implements OnTouchListener{  
    private Bitmap bg_on, bg_off, slipper_btn;  
    private String mOnText = "開啟";
    private String mOffText = "關閉";
    /**
     * 按下時的x和當前的x
     */  
    private float downX, nowX;  
      
    /**
     * 記錄使用者是否在滑動
     */  
    private boolean onSlip = false;  
      
    /**
     * 當前的狀態
     */  
    private boolean nowStatus = false;  
      
    /**
     * 監聽介面
     */  
    private OnChangedListener listener;  
      
      
    public WiperSwitch(Context context) {  
        super(context);  
        init();  
    }  
 
    public WiperSwitch(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        init();  
    }  
      
    public void init(){  
        //載入圖片資源  
        bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bkg_switch1);  
        bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.switch_bkg_switch1);  
        slipper_btn = BitmapFactory.decodeResource(getResources(), R.drawable.switch_btn_slip1);  
        setOnTouchListener(this);  
    }  
      
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        Matrix matrix = new Matrix();  
        Paint paint = new Paint();  
        float x = 0;  
          
        //根據nowX設定背景,開或者關狀態  
        if (nowX < (bg_on.getWidth()/2)){  
              canvas.drawBitmap(bg_off, matrix, paint);//畫出關閉時的背景
              paint.setColor(Color.WHITE);
              paint.setFakeBoldText(true);
              paint.setTextSize(40);
              canvas.drawText(mOffText, (bg_off.getWidth()-slipper_btn.getWidth())/2, (bg_off.getHeight()+30)/2, paint);
        }else{  
            canvas.drawBitmap(bg_on, matrix, paint);//畫出開啟時的背景   
            canvas.drawBitmap(bg_on, matrix, paint);//畫出開啟時的背景   
            paint.setColor(Color.WHITE);
            paint.setFakeBoldText(true);
            paint.setTextSize(40);
            canvas.drawText(mOnText,(bg_on.getWidth()-slipper_btn.getWidth())/2, (bg_on.getHeight()+30)/2, paint);
        }  
          
        if (onSlip) {//是否是在滑動狀態,    
            if(nowX >= bg_on.getWidth())//是否划出指定範圍,不能讓滑塊跑到外頭,必須做這個判斷  
                x = bg_on.getWidth() - slipper_btn.getWidth()/2;//減去滑塊1/2的長度  
            else  
                x = nowX - slipper_btn.getWidth()/2;  
        }else {  
            if(nowStatus){//根據當前的狀態設定滑塊的x值  
                x = bg_on.getWidth() - slipper_btn.getWidth();  
            }else{  
                x = 0;  
            }  
        }  
          
        //對滑塊滑動進行異常處理,不能讓滑塊出界  
        if (x < 0 ){  
            x = 0;  
        }  
        else if(x > bg_on.getWidth() - slipper_btn.getWidth()){  
            x = bg_on.getWidth() - slipper_btn.getWidth();  
        }  
          
        //畫出滑塊  
        canvas.drawBitmap(slipper_btn, x , 0, paint);   
    }  
 
    @Override  
    public boolean onTouch(View v, MotionEvent event) {  
        switch(event.getAction()){  
        case MotionEvent.ACTION_DOWN:{  
            if (event.getX() > bg_off.getWidth() || event.getY() > bg_off.getHeight()){  
                return false;  
            }else{  
                onSlip = true;  
                downX = event.getX();  
                nowX = downX;  
            }  
            break;  
        }  
        case MotionEvent.ACTION_MOVE:{  
            nowX = event.getX();  
            break;  
        }  
        case MotionEvent.ACTION_UP:{  
            onSlip = false;  
            if(event.getX() >= (bg_on.getWidth()/2)){  
                nowStatus = true;  
                nowX = bg_on.getWidth() - slipper_btn.getWidth();  
            }else{  
                nowStatus = false;  
                nowX = 0;  
            }  
              
            if(listener != null){  
                listener.OnChanged(WiperSwitch.this, nowStatus);  
            }  
            break;  
        }  
        }  
        //重新整理介面  
        invalidate();  
        return true;  
    }  
      
      
      
    /**
     * 為WiperSwitch設定一個監聽,供外部調用的方法
     * @param listener
     */  
    public void setOnChangedListener(OnChangedListener listener){  
        this.listener = listener;  
    }  
      
    /**
     * 設定滑動開關的初始狀態,供外部調用
     * @param checked
     */  
    public void setChecked(boolean checked){  
        if(checked){  
            nowX = bg_off.getWidth();  
        }else{  
            nowX = 0;
        }  
        nowStatus = checked;  
    }  
    /**
     * 回調介面
     * @author len
     *
     */  
    public interface OnChangedListener {  
        public void OnChanged(WiperSwitch wiperSwitch, boolean checkState);  
    }  
 
 

 

android 自訂滑動按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.