Slidable ToggleButton)

Source: Internet
Author: User

17:25:01

I saw an article about the slidable ToogleButton on the Internet. I thought it was good with code, but it didn't meet my requirements. So I changed it based on his code. (I can't find the original text, so I have to paste the code .)

Source code: http://download.csdn.net/detail/zshq280017423/4240703

Here are two images:

Then run the Code:

The main class is SlipButton. java.

                          DownX, NowX;       btn_on_left =        btn_off_left =        boolean NowChoose = ;           boolean OnSlip = ;      boolean isChgLsnOn =                                                init() {          bg_on =          bg_off =          slip_btn =           btn_off_left = bg_off.getWidth() -          setOnTouchListener();           onDraw(Canvas canvas) {           Matrix matrix =          Paint paint =                     (NowX < (bg_on.getWidth() / )) {              x = NowX - slip_btn.getWidth() /              canvas.drawBitmap(bg_off, matrix, paint);         }              x = bg_on.getWidth() - slip_btn.getWidth() /              canvas.drawBitmap(bg_on, matrix, paint);            (OnSlip) {              (NowX >= bg_on.getWidth()) {                 x = bg_on.getWidth() - slip_btn.getWidth() / ;             }   (NowX <                  x =              }                  x = NowX - slip_btn.getWidth() /           }  {              (NowChoose) {                 x =                 canvas.drawBitmap(bg_on, matrix, paint);             }                  x =                         x =             isChecked = !            (x < ) {             x =          }   (x > bg_on.getWidth() -             x = bg_on.getWidth() -          canvas.drawBitmap(slip_btn, x, , paint);         boolean onTouch(View v, MotionEvent           (.getAction()) {              MotionEvent.ACTION_DOWN:                  (.getX() >                         || .getY() >                                        OnSlip =                  DownX =                  NowX =                                MotionEvent.ACTION_MOVE:                 Log.d(,  +                  Log.d(,  +                  NowX =                  boolean LastChoose =                   (NowX >= (bg_on.getWidth() /                      NowChoose =                  }                      NowChoose =                     (isChgLsnOn && (LastChoose != NowChoose)) {                                   MotionEvent.ACTION_UP:                 OnSlip =                                         invalidate();                   SetOnChangedListener(OnChangedListener l) {         isChgLsnOn =          ChgLsn =                                   .isChecked =         NowChoose =          onMeasure( widthMeasureSpec,            measuredHeight =          measuredWidth =          measureHeight(                                            measureWidth(                                   }

The code is relatively simple and the comments are more detailed.

Let's talk about some questions:

1. What are the values of event. getX () and event. getY () obtained in the onTouch () method? What is the value range?

At first, I thought that no matter where our SlipButton is placed, the value of event. getX () is the coordinate value relative to the screen ,~~ In fact, it is wrong. event. getX () will get the value only when your touch point falls within the layout range of the current SlipButton view. However, the value range is not only the size of your SlipButton view, because once your touch point is within the view range, the touch point can be moved to any place outside the View, therefore, the value range should be full screen, so we made the following judgment in the Code:

  (.getX() >              || .getY() >        }

2. Why does measureWidth () and measureHeight () return a fixed value?

First, according to the purpose, we do not need to adjust the size of the custom SlipButton View by the caller, because the ToggleButton itself serves as a switch and should be consistent in the application, so I asked the two methods to return a fixed value. For a detailed description of the onMeasure () method root, see my other article: http://www.cnblogs.com/wlrhnh/p/3479928.html

Download the source code. Click here.

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.