[Android] toggle button-Custom control-drag effect

Source: Internet
Author: User

Overrides the View 's ontouchevent () method, passing in the motionevent Object

Call the getaction () method of the motionevent object to get the current action

Switch to determine the current action

The event for Motionevent.action_down is the first time the finger touches the screen

The event for Motionevent.action_move is that the finger moves on the screen

The event for motionevent.action_up is the finger leaving the screen

When the finger touches the screen

Defines the last coordinate of the finger lastx

Call the GetX () method of the motionevent object to get The value of LASTX

When the finger moves on the screen

Defines the distance the finger moves horizontally dis

Call GetX ()-lastx is the distance to move

The left side of the definition slide button is the distance to move

Judge Slidebtnleft in a reasonable position,0 to the width of the background graph - The width of the sliding button

Call the invalidate () method to refresh the view

The OnClick event and The ontouchevent are conflicting

Define a flag Isdrag variable, and if any drag occurs, assign this variable to true

This variable is judged in the Oncllick () method

When the finger lifts up

Determines the current slidebtnleft to determine whether the current button is open or closed

Slidebtnleft can judge the current state by comparing it to half the maxleft .

 Packagecom.tsh.myswitchbtn;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMytogglebtnextendsViewImplementsOnclicklistener {//Background Image    PrivateBitmap Bitmapbackground; //button Picture    PrivateBitmap bitmapbtn; PrivatePaint paint; /*** used in Layout files *@paramContext *@paramAttrs*/     Publicmytogglebtn (Context context, AttributeSet attrs) {Super(context, attrs);    Initview (); }    /*** Initialize View*/    Private voidInitview () {Bitmapbackground=Bitmapfactory.decoderesource (Getresources (), r.drawable.switch_background); BITMAPBTN=Bitmapfactory.decoderesource (Getresources (), R.drawable.slide_button); Paint=NewPaint (); Paint.setantialias (true); //Click eventsSetonclicklistener ( This); }    /*** Calculate Size*/@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec)    {setmeasureddimension (Bitmapbackground.getwidth (), Bitmapbackground.getheight ()); }    //Current status    Private BooleanCurrentstate=false; //the current left of the slide button    Private floatSlidebtnleft=0; /*** Draw View*/@Overrideprotected voidOnDraw (canvas canvas) {//Draw BackgroundCanvas.drawbitmap (bitmapbackground, 0, 0, paint); //Draw Slide ButtonsCanvas.drawbitmap (bitmapbtn, Slidebtnleft, 0, paint); }    Private Booleanisdrag=false; /*** Click events*/@Override Public voidOnClick (View v) {//Resolve conflicts with mobile events        if(!Isdrag) {CurrentState= !CurrentState;        Flushstate (); }    }    Private intLastx; /*** Touch Events*/    Private intFirstx; @Override Public Booleanontouchevent (Motionevent event) {Super. Ontouchevent (event); Switch(Event.getaction ()) {//Finger Press             CaseMotionEvent.ACTION_DOWN:firstX=lastx= (int) Event.getx (); Isdrag=false;  Break; //Finger Movement             CaseMotionevent.action_move://Resolve conflicts with Click events                if(Math.Abs (Event.getx ()-firstx) >5) {Isdrag=true; }                                intDis= (int) Event.getx ()-Lastx; Slidebtnleft=slidebtnleft+dis; Lastx=(int) Event.getx ();  Break; //Finger lift up             Casemotionevent.action_up:if(Isdrag) {intMaxleft =bitmapbackground.getwidth ()-bitmapbtn.getwidth (); if(Slidebtnleft >= MAXLEFT/2) {CurrentState=true; } Else{currentstate=false;                } flushstate (); }             Break;                } flushview (); return true; }    /*** Refresh Status*/    Private voidflushstate () {if(CurrentState = =true) {Slidebtnleft=bitmapbackground.getwidth ()-bitmapbtn.getwidth (); } Else{slidebtnleft= 0;    } invalidate (); }    /*** Refresh View*/    Private voidFlushview () {intMaxleft=bitmapbackground.getwidth ()-bitmapbtn.getwidth (); Slidebtnleft= (slidebtnleft>0)? slidebtnleft:0; Slidebtnleft= (slidebtnleft<maxleft)?Slidebtnleft:maxleft;    Invalidate (); }}

[Android] toggle button-Custom control-drag effect

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.