Android custom View-slide Switch

Source: Internet
Author: User

MainActivity is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> package cc. testview3; import cc. testview3.SwitchView. switchChangedListener; import android. OS. bundle; import android. widget. toast; import android. app. activity;/*** Demo Description: * Sliding switch for custom View ** Test Device: * Resolution: 480x854 */public class MainActivity extends Activity {private SwitchView mSwitchView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (sav EdInstanceState); setContentView (R. layout. main); init ();} private void init () {mSwitchView = (SwitchView) findViewById (R. id. switchView); mSwitchView. initSwitchStatus (true); mSwitchView. setOnSwitchChangedListener (new SwitchChangedListenerImpl ();} private class SwitchChangedListenerImpl implements SwitchChangedListener {@ Overridepublic void OnChanged (boolean currentStatus) {Toast. makeText (MainActivity. This, "currentIsOff? --> "+ CurrentStatus, Toast. LENGTH_SHORT). show ();}}}


SwitchView:

Package cc. testview3; 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. util. attributeSet; import android. view. motionEvent; import android. view. view; public class SwitchView extends View {private Bitmap mOnBitmap; private Bitmap mOffBitmap; private B Itmap mDotBitmap; private float currentX; private boolean currentIsSlipping = false; private boolean currentIsOff; private SwitchChangedListener listener; private int dotWidth; private int switchWidth; public SwitchView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); initSwitchView ();} public SwitchView (Context context, AttributeSet attrs) {super (context, Attrs); initSwitchView ();} public SwitchView (Context context) {super (context); initSwitchView ();} private void initSwitchView () {mOnBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. on); mOffBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. off); mDotBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. dot); dotWidth = mDotBitmap. getWidth (); switchWidth = mOnBitmap. getWidth (); This. setOnTouchListener (new TouchListenerImpl ();} public void initSwitchStatus (boolean isOff) {if (isOff) {currentX = switchWidth;} else {currentX = 0;} currentIsOff = isOff ;} @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec);} @ Overrideprotected void onLayout (boolean changed, int left, int top, int right, int bottom) {super. OnLayout (changed, left, top, right, bottom) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); Matrix matrix = new Matrix (); Paint paint = new Paint (); // first draws the background of the switch (Off/On) if (currentIsOff) {canvas. drawBitmap (mOffBitmap, matrix, paint);} else {canvas. drawBitmap (mOnBitmap, matrix, paint);} // draw the slider. // 1. Slide (if ), the left of the slider is the changing currentX // when the sliding stops after the 2 fingers are raised (else. at this time, the switch should be on or off on the left and right. // if (currentIsS Lipping) {canvas. drawBitmap (mDotBitmap, currentX, 17, paint);} else {if (currentIsOff) {canvas. drawBitmap (mDotBitmap, currentX-dotWidth, 17, paint);} else {canvas. drawBitmap (mDotBitmap, currentX, 17, paint) ;}} private class TouchListenerImpl implements OnTouchListener {@ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: break; case Mot IonEvent. ACTION_MOVE: currentIsSlipping = true; currentX = event. getX (); if (currentX> switchWidth/2) {currentIsOff = true;} else {currentIsOff = false;} // prevents cross-border if (event. getX ()> switchWidth-dotWidth) {currentX = switchWidth-dotWidth; currentIsOff = true;} // prevents cross-border if (event. getX () <0) {currentX = 0; currentIsOff = false;} // redraw !!! Invalidate (); break; case MotionEvent. ACTION_UP: currentIsSlipping = false; currentX = event. getX (); // if (if) has exceeded the normal length of the switch, the switch is in the off state. // otherwise (else) make it open. if (currentX> = switchWidth/2) {currentX = switchWidth; currentIsOff = true;} else {currentX = 0; currentIsOff = false;} if (mSwitchChangedListener! = Null) {mSwitchChangedListener. OnChanged (currentIsOff) ;}// redraw !!! Invalidate (); break; default: break;} return true ;}// interface public interface SwitchChangedListener {public void OnChanged (boolean currentIsOff);} public void setOnSwitchChangedListener (SwitchChangedListener switchChangedListener) {this. mSwitchChangedListener = switchChangedListener ;}}

Main. xml is as follows:

         
      
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.