滑块按钮

滑块按钮的实现

运行的效果:


用到的图片:(放置到drawable_xhdpi目录下)

           http://img.my.csdn.net/uploads/201302/13/1360737705_4656.png

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;           //是打开还是关闭

    private boolean isSliding = false;      //是否正在滑动

   

    public SlipButton(Context context,AttributeSetattrs) {

       super(context); 

       init();

    }  

   

    /**

     * 初始化参数

     */

    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();

    }

   

    /**

     * 重写viewonDraw函数

     */

    @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);  

    }

   

    /**

     * 控制滑块的移动,并且会重新设置移动的位置的参数

     */

    @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();         //此函数是view自带的函数,用于调用onDraw函数重新绘制

       return true;

    }  

}

你可能感兴趣的:(滑块按钮)