Android Custom View Implementation Click and double click events

Source: Internet
Author: User

Custom View,

1. Customize a runnable thread toucheventcountthread to count the number of clicks within 500ms

2. Call the above thread in Ontouchevent in MyView

3. Customize a handler, in Toucheventhandler processing statistics to the Click event, click, double Tap, three click, can handle

The core code is as follows:

 Public classMyViewextendsView {...//count clicks in 500msToucheventcountthread Mintoucheventcount =NewToucheventcountthread (); //perform clicks or double-click events based on the number of clicks that toucheventcountthread countsToucheventhandler Mtoucheventhandler =NewToucheventhandler (); @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down: If (0 = = Mintoucheventcount.touchcount) //The first time you press, start counting                    postdelayed (Mintoucheventcount,);  Break;  Casemotionevent.action_up://A single Click event must be pressed and lifted, there must be a hold down, so only need to handle in action_up                mintoucheventcount.touchcount++;
Break; CaseMotionevent.action_move: Break; CaseMotionevent.action_cancel: Break; default: Break; } return Super. Ontouchevent (event); } Public class Toucheventhandler extendsHandler {@Override Public voidhandlemessage (Message msg) {Toast.maketext (Mcontext,"Touch "+ msg.arg1 +" time.", Toast.length_short). Show (); } } Public class Toucheventcountthread ImplementsRunnable { Public intTouchcount = 0; @Override Public voidrun () {Message msg=NewMessage (); msg.arg1 = Touchcount; mtoucheventhandler.sendmessage (msg); Touchcount = 0; } } ......}

All code

 PackageCom.carloz.test.myapplication.view;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.widget.Toast;ImportCOM.CARLOZ.TEST.MYAPPLICATION.R;/*** Created by Root on 15-11-9.*/ Public classMyViewextendsView {PrivatePaint Mpaint =NewPaint (); Private BooleanMnotdestroy =true; Private intMCount = 0; PrivateMyThread MyThread;    Bitmap Bitmap; //Attrs    PrivateString MText; Private BooleanMstartchange;    Context Mcontext;  PublicMyView (Context context) {Super(context);    Init (); }     PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); TypedArray Ta=context.obtainstyledattributes (Attrs, R.styleable.myview); MText=ta.getstring (R.styleable.myview_text); Mstartchange= Ta.getboolean (R.styleable.myview_startchange,false); LOG.D ("ASDF", "mtext=" + MText + ", mstartchange=" +Mstartchange);        Ta.recycle ();    Init (); } @Overrideprotected voidonfinishinflate () {Super. Onfinishinflate (); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @Overrideprotected voidOnLayout (BooleanChangedintLeftintTopintRightintbottom) {        Super. OnLayout (changed, left, top, right, bottom); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Mpaint.settextsize (50); Canvas.drawtext (MText+ mcount++, 20f, 100f, Mpaint);        Canvas.save (); Canvas.rotate (GetWidth ()/2, GetHeight ()/2);        Canvas.drawbitmap (Bitmap, 20f, 50f, Mpaint);        Canvas.restore (); if(NULL==myThread) {MyThread=NewMyThread ();        Mythread.start (); }} @Override Public Booleandispatchtouchevent (motionevent ev) {return Super. dispatchtouchevent (EV); } @Overrideprotected voidOnattachedtowindow () {Super. Onattachedtowindow (); Mnotdestroy=true; } @Overrideprotected voidOndetachedfromwindow () {Mnotdestroy=false; Super. Ondetachedfromwindow (); }    //count clicks in 500msToucheventcountthread Mintoucheventcount =NewToucheventcountthread (); //perform clicks or double-click events based on the number of clicks that toucheventcountthread countsToucheventhandler Mtoucheventhandler =NewToucheventhandler (); @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down:if(0 = = Mintoucheventcount.touchcount)//The first time you press, start countingPostdelayed (Mintoucheventcount, 500);  Break;  Casemotionevent.action_up://A single Click event must be pressed and lifted, there must be a hold down, so only need to handle in action_upmintoucheventcount.touchcount++;//                 Break;  CaseMotionevent.action_move: Break;  CaseMotionevent.action_cancel: Break; default:                 Break; }        return Super. Ontouchevent (event); }     Public classToucheventhandlerextendsHandler {@Override Public voidhandlemessage (Message msg) {Toast.maketext (Mcontext,"Touch" + Msg.arg1 + "time.", Toast.length_short). Show (); }    }     Public classToucheventcountthreadImplementsRunnable { Public intTouchcount = 0; @Override Public voidrun () {Message msg=NewMessage (); Msg.arg1=Touchcount;            Mtoucheventhandler.sendmessage (msg); Touchcount= 0; }    }    classMyThreadextendsThread {@Override Public voidrun () {Super. Run ();  while(Mnotdestroy) {if(Mstartchange) {postinvalidate (); Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); }                }            }        }    }     Public voidinit () {Mcontext=GetContext (); Bitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); }     Public voidSetText (String mText) { This. MText =MText; }     Public voidSetstartchange (BooleanMstartchange) {         This. Mstartchange =Mstartchange; }     Public BooleanGetstartchange () {return  This. Mstartchange; }}
View Code

Android Custom View Implementation Click and double click events

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.