Zhao yazhi _ name card clip (1) _ custom SlipButton and Zhao yazhi slipbutton
Reprinted please indicate the source: http://blog.csdn.net/zhaoyazhi2129/article/details/38732165
As follows:
Place image resources in the res/drawable-hdip folder
In the layout file, the file that references the custom SlipButton
<com.cards.commom.SlipButton android:id="@+id/slipBtn_jzmm" android:layout_width="70dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginTop="10dp" android:onClick="@string/act_click" />
SlipButton File
Package com. cards. commom; import com. cards. r; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. graphics. rect; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; publi C class SlipButton extends View implements OnTouchListener {private boolean NowChoose = false; // record whether the current button is enabled; true indicates that it is enabled; flase indicates that private boolean isChecked is disabled; private boolean OnSlip = false; // record whether the user is in the sliding variables private float DownX and NowX; // The x when the user is pressed, the current xprivate Rect Btn_On and Btn_Off; // when the user is on and off, the Rect of the cursor. private boolean isChgLsnOn = false; private OnChangedListener ChgLsn; private Bitmap bg_on, bg_off, slip_btn; public S LipButton (Context context) {super (context); init ();} public SlipButton (Context context, AttributeSet attrs) {super (context, attrs); init ();} public SlipButton (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init () ;}private void init () {// initialize bg_on = BitmapFactory. decodeResource (getResources (), R. drawable. zhuce_off); bg_off = BitmapFactory. decodeResource (getResources (), R. drawable. zhuce_on); slip_btn = BitmapFactory. decodeResource (getResources (), R. drawable. zhuce_btn); Btn_On = new Rect (0, 0, weight (), slip_btn.getHeight (); Btn_Off = new Rect (bg_off.getWidth ()-weight (), 0, bg_off.getWidth (), slip_btn.getHeight (); setOnTouchListener (this); // set the listener. You can also directly rewrite OnTouchEvent} @ Overrideprotected void onDraw (Canvas canvas Canvas) {// The drawing function super. onDraw (canvas); Ma Trix matrix = new Matrix (); Paint paint = new Paint (); float x; if (NowX <(bg_on.getWidth ()/2 )) // sliding to the background of the first half and the second half is different. Here, we will judge {x = NowX-slip_btn.getWidth ()/2; canvas. drawBitmap (bg_off, matrix, paint); // draws the background when it is disabled} else {x = bg_on.getWidth ()-slip_btn.getWidth ()/2; canvas. drawBitmap (bg_on, matrix, paint); // draws the background when open} if (OnSlip) // whether it is in the slide status, {if (NowX> = bg_on.getWidth ()) // specifies whether to specify a range. The cursor cannot be redirected to an external header. Therefore, the cursor must be x = B. G_on.getWidth ()-slip_btn.getWidth ()/2; // subtract the length of the cursor 1/2... else if (NowX <0) {x = 0;} else {x = NowX-slip_btn.getWidth ()/2 ;}} else {// non-slide status if (NowChoose) // set the cursor position {x = Btn_Off.left; canvas. drawBitmap (bg_on, matrix, paint); // when the initial status is true, the open status picture should be drawn.} elsex = Btn_On.left;} if (isChecked) {canvas. drawBitmap (bg_on, matrix, paint); x = Btn_Off.left; isChecked =! IsChecked;} if (x <0) // exception judgment on the cursor position... x = 0; else if (x> bg_on.getWidth ()-slip_btn.getWidth () x = bg_on.getWidth ()-slip_btn.getWidth (); canvas. drawBitmap (slip_btn, x, 0, paint); // draw the cursor .} public boolean onTouch (View v, MotionEvent event) {switch (event. getAction () // execute code {case MotionEvent according to the action. ACTION_MOVE: // slide NowX = event. getX (); break; case MotionEvent. ACTION_DOWN: // press if (event. getX ()> bg_on.get Width () | event. getY ()> bg_on.getHeight () return false; OnSlip = true; DownX = event. getX (); NowX = DownX; break; case MotionEvent. ACTION_CANCEL: // OnSlip = false; boolean choose = NowChoose; if (NowX >=( bg_on.getWidth ()/2) {NowX = bg_on.getWidth ()-slip_btn.getWidth () /2; NowChoose = true;} else {NowX = NowX-slip_btn.getWidth ()/2; NowChoose = false;} if (isChgLsnOn & (choose! = NowChoose) // If the listener is set, call the method .. chgLsn. onChanged (NowChoose); break; case MotionEvent. ACTION_UP: // release OnSlip = false; boolean LastChoose = NowChoose; if (event. getX ()> = (bg_on.getWidth ()/2) {NowX = bg_on.getWidth ()-slip_btn.getWidth ()/2; NowChoose = true;} else {NowX = NowX-distance () /2; NowChoose = false;} if (isChgLsnOn & (LastChoose! = NowChoose) // If the listener is set, call the method .. chgLsn. onChanged (NowChoose); break; default:} invalidate (); // redraw control return true;} public void SetOnChangedListener (OnChangedListener l) {// sets the listener, when the status is changed, isChgLsnOn = true; ChgLsn = l;} public interface OnChangedListener {abstract void OnChanged (boolean CheckState);} public void setCheck (boolean isChecked) {this. isChecked = isChecked; NowChoose = isChecked ;}}