http://www.apkbus.com/forum.php?mod=viewthread&tid=55593

 

先看一下效果图:
. 1334847830_3065.jpg
1334847960_5778.jpg

  1. public class SlipButton extends View {  
  2.   
  3.     private boolean nowChoose = false;//记录当前按钮是否打开,true为打开,flase为关闭  
  4.     private boolean onSlip = false;//记录用户是否在滑动的变量  
  5.     private float nowX;//按下时的x,当前的x
  6.     private float downX;
  7.     private float baseX;
  8.     private OnChangedListener ChgLsn;  
  9.     private Bitmap slip_btn;
  10.     private float centerX;
  11.    
  12.     private float yes_text_width;
  13.     private float no_text_width;
  14.    
  15.     private int btnWdith;
  16.     private String[] messages;
  17.    
  18.     private Paint paint;
  19.       
  20.     public SlipButton(Context context) {  
  21.         super(context);   
  22.         init();  
  23.     }  
  24.   
  25.     public SlipButton(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         init();  
  28.     }  
  29.   
  30.     private void init() {
  31.         paint = new Paint();
  32.         paint.setAntiAlias(true);
  33.         paint.setColor(Color.BLACK);
  34.         paint.setTextSize(14);
  35.         setMessages(new String[]{"Yes", "No"});
  36.     }
  37.    
  38.     public void setButtonBitmap(Bitmap bitmap) {
  39.             if (bitmap == null) {
  40.                     return;
  41.             }
  42.             slip_btn = bitmap;
  43.         btnWdith = slip_btn.getWidth() / 3;
  44.         centerX = btnWdith / 2;
  45.     }
  46.    
  47.     public void setTextColor(int color) {
  48.             paint.setColor(color);
  49.     }
  50.    
  51.     public void setTextSize(int size) {
  52.             paint.setTextSize(size);
  53.     }
  54.    
  55.     public void setMessages(String[] messages) {
  56.             this.messages = messages;
  57.             yes_text_width = paint.measureText(messages[0]);
  58.         no_text_width = paint.measureText(messages[1]);
  59.     }
  60.    
  61.     public void setStatusOn(boolean on) {
  62.             nowChoose = on;
  63.     }
  64.    
  65.     public boolean getStatusOn() {
  66.             return nowChoose;
  67.     }
  68.    
  69.     public void setOnChangedListener(OnChangedListener l) {
  70.         ChgLsn = l;  
  71.     }
  72.       
  73.     @Override  
  74.     protected void onDraw(Canvas canvas) {
  75.         super.onDraw(canvas);
  76.         if (slip_btn == null) {
  77.                     return;
  78.             }
  79.         
  80.         float minX = centerX;
  81.         float maxX = getMeasuredWidth() - centerX;
  82.         
  83.         if (onSlip) {
  84.                 nowX = baseX - (downX - nowX);
  85.                 nowX = Math.min(Math.max(minX, nowX), maxX);
  86.         } else {
  87.                 if (nowChoose) {
  88.                         nowX = maxX;
  89.                 } else {
  90.                         nowX = minX;
  91.                 }
  92.                 baseX = nowX;
  93.         }
  94.             canvas.drawBitmap(slip_btn, nowX - centerX - btnWdith, 0, paint);//画出游标.
  95.            
  96.         int marginX1 = (int)(btnWdith - yes_text_width) / 2;
  97.         canvas.drawText(messages[0], nowX - centerX - marginX1 - yes_text_width, 22, paint);
  98.             
  99.         int marginX2 = (int)(btnWdith - no_text_width) / 2;
  100.         canvas.drawText(messages[1], nowX + centerX + marginX2, 22, paint);
  101.     }  
  102.   
  103.   
  104.     @Override
  105.         public boolean onTouchEvent(MotionEvent event) {
  106.             if (slip_btn == null) {
  107.                     return false;
  108.             }
  109.             
  110.         switch(event.getAction())//根据动作来执行代码  
  111.         {  
  112.         case MotionEvent.ACTION_MOVE://滑动
  113.                 nowX = event.getX();
  114.                 float moveX = downX - nowX;
  115.                 if (Math.abs(moveX) > 5) {
  116.                         onSlip = true;
  117.                 }
  118.             break;  
  119.         case MotionEvent.ACTION_DOWN://按下  
  120.                 downX = event.getX();
  121.             break;  
  122.         case MotionEvent.ACTION_UP://松开  
  123.                 boolean LastChoose = nowChoose;
  124.                 if (onSlip) {
  125.                         moveX = downX - event.getX();
  126.                         if (nowChoose) {
  127.                                 if ((moveX > 0) && (moveX >= centerX))
  128.                                         nowChoose = !nowChoose;
  129.                         } else {
  130.                                 if ((moveX < 0) && (Math.abs(moveX) >= centerX))
  131.                                         nowChoose = !nowChoose;
  132.                         }
  133.                 } else {
  134.                         nowChoose = !nowChoose;
  135.                 }
  136.             onSlip = false;
  137.             if ((ChgLsn != null) && (LastChoose != nowChoose)) {
  138.                     if (nowChoose) {
  139.                             ChgLsn.onChanged(nowChoose, messages[0]);
  140.                     } else {
  141.                             ChgLsn.onChanged(nowChoose, messages[1]);
  142.                     }
  143.             }
  144.             break;  
  145.         default:
  146.                 break;
  147.         }  
  148.         invalidate();//重画控件  
  149.         return true;
  150.         }
  151.    
  152.     public interface OnChangedListener {
  153.             public void onChanged(boolean on, String value);
  154.     }
  155. }
  156.  
复制代码