Android ontouchevent Events

Source: Internet
Author: User

According to the user's touch to determine the direction of the slide, select Eject Popupwindow or dialog, you can also switch the interface, define the toggle animation


Define interfaces:

/** * According to the Angle of the sliding range sliding direction * @author Lanyan * */public interface Oneventlistener {/** *←*/void onleft ();/** *↑*/void OnTop ();/** *→*/void onright ();/** *↓*/void Onbottom ();/** *  */void Onnort Heast ();/** *  */void onnorthwest ();/** *  */void onsouthwest ();/** *  */void onsoutheast ();/** * Touch screen */void OnTouch ();/** * Event Distribution */eventtype oneventdistribute ();

Method Invoke Tool Class:

Import Android.view.motionevent;import Android.view.view;public class Oneventtouchfactory {private static Oneventtouchfactory mcurrent;private static oneventlistener mlistener;private int mlasteventx;private int MLastEventY;  Private EventType mtype;private int mdistance = 50;private double cos;private double result;private EventType mCurrentType = Null;public Oneventtouchfactory () {}public static oneventtouchfactory getinstance (Oneventlistener listener) { Mlistener = listener;if (mcurrent = = null) {mcurrent = new oneventtouchfactory ();} return mcurrent;} public boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubboolean Status=false;mlistener.ontou CH (); int eventx = (int) event.getx (), int eventy = (int) event.gety (); int action = Event.getaction (); if (action = = Motionev Ent. Action_down) {mlasteventx = Eventx;mlasteventy = Eventy;mcurrenttype = Mlistener.oneventdistribute (); status=true;} else if (action = = motionevent.action_move) {if (Mcurrenttype = = Eventtype.normaL) {cos = Getangle (eventx, Eventy, Mlasteventx, mlasteventy); result = Getdistance (Mlasteventx, Mlasteventy, EventX, event Y);/** * eeventtype:left←: [157.5,202.5] top↑: [67.5,112.5] Right *→: [337.5,360]&&[0,22.5] bottom↓: [247.5,2 92.5] Northwest: * [112.5,157.5]┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅southeast: [292.5,337.5]┇* Southwest: [202.5,247.5]┇northeast: [22.5,67.5]┇┇* velocityxy---------------┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅*┅┅┅┅┅┇*/if (cos >= 0 && cos <= 22 .5) | | (Cos >= 337.5 && cos <= 360)) {mtype = Eventtype.left;} else if (Cos > 22.5 && cos <= 67.5) {mtype = eventtype.nortwest;} else if (cos >= 67.5 && cos <= 112.5) {mtype = Eventtype.top;} else if (cos > 112.5 && cos < 157.5) {mtype = Eventtype.northeast;} else if (cos >= 157.5 && cos <= 202.5) {mtype = Eventtype.right;} else if (cos > 202.5 && cos < 247.5) {mtype = Eventtype.southeast;} else if (cos >= 247.5 && cos <= 292.5) {mtype = Eventtype.bottom;} else if (cos > 292.5 && cos < 337.5) {mtype = eventtype.southwest;}  Status=true;} else if (Mcurrenttype = = Eventtype.complete) {status= false;}} else if (action = = motionevent.action_up) {if (Mtype = = Eventtype.right && result > mdistance) {mlistener.onrig HT ();} else if (mtype = = Eventtype.top && result > Mdistance) {mlistener.ontop ();} else if (mtype = = Eventtype.northea St && result > Mdistance) {mlistener.onnortheast ();} else if (mtype = = Eventtype.nortwest && result ; Mdistance) {mlistener.onnorthwest ();} else if (mtype = = Eventtype.left && result > mdistance) {mlistener.onlef T ();} else if (mtype = = Eventtype.southeast && result > Mdistance) {mlistener.onsoutheast ();} else if (mtype = = Event  Type.southwest && result > Mdistance) {mlistener.onsouthwest ();} else if (mtype = = Eventtype.bottom && Result > Mdistance) {mlistener.onbottom ();} Status= TRue;} return status;} public double getangle (int px1, int py1, int px2, int py2) {//two point x, y value int x = px2-px1;int y = py2-py1;double hypotenus E = math.sqrt (Math.pow (x, 2) + Math.pow (Y, 2));//hypotenuse length double cos = x/hypotenuse;double radian = math.acos (COS);//Calculate radian Do Uble angle =/(Math.pi/radian);//calculate angle in radians if (Y < 0) {angle = + + (180-angle);} else if ((y = = 0) && (x < 0)) {angle = 180;} return angle;}  /** * Get two points distance * * @param x1 * @param y1 * @param x2 * @param y2 * @return */public double getdistance (int x1, int y1, int x2, int y2) {Double d = (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1); return math.sqrt (d);}  Public enum EventType {left, top, right, bottom, Nortwest, Southeast, Southwest, northeast,/** * Event not distributed */normal,/** * Distribute */complete} completely downward

Baseactivity class:

Import Com.example.base.oneventtouchfactory.eventtype;import Android.app.activity;import android.content.Intent; Import Android.os.bundle;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.toast;public abstract class Baseactivity extends Activity Implements Ontouchlistener, oneventlistener{protected class<?> mleftclass=null;protected Class<?> mrightclass=null;protected class<?> mtopclass=null;protected class<?> mBottomClass=null;protected Class <?> mnortheastclass=null;protected class<?> mnorthwestclass=null;protected class<?> msoutheastclass=null;protected class<?> msouthwestclass=null; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); SetTheme (); Setcontentview (Getlayoutresid ()); Configclass (); initialization ();} public abstract int Getlayoutresid ();p ublic void SetTheme () {}public void Configclass () {}public void initialization () {}/***********************************ontouch************************************ /@Overridepublic Boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubif ( Oneventtouchfactory.getinstance (This). OnTouch (V, event)) {return true;} return false;} @Overridepublic void Onleft () {//Todo auto-generated method stub} @Overridepublic void OnTop () {//Todo auto-generated met Hod stub} @Overridepublic void Onright () {//Todo auto-generated method stub} @Overridepublic void Onbottom () {//Todo auto- Generated method stub} @Overridepublic void Onnortheast () {//TODO auto-generated method stub} @Overridepublic void Onnorth West () {//Todo auto-generated method stub} @Overridepublic void Onsouthwest () {//Todo auto-generated method stub} @Overrid epublic void Onsoutheast () {//Todo auto-generated method stub} @Overridepublic void OnTouch () {//Todo auto-generated meth OD stub} @Overridepublic eventtype Oneventdistribute () {//TODO auto-generated method StuBreturn Eventtype.normal;} /***********************************method*********************************************/public void StartActivity (class<?> cls) {Intent intent=new Intent (); Intent.setclass (this, CLS); startactivity (Intent);} public void StartActivity (class<?> cls,boolean isfinish) {Intent intent=new Intent (); Intent.setclass (this, CLS); StartActivity (intent); if (isfinish) {finish ();}} public void Startactivitywithanimation (class<?> cls,int enteranim,int exitanim) {Intent intent=new Intent (); Intent.setclass (this, CLS); startactivity (intent); Overridependingtransition (Enteranim, Exitanim);} public void StartActivity (class<?> cls,boolean isfinish,int enteranim,int exitanim) {Intent intent=new Intent (); Intent.setclass (this, CLS); startactivity (intent); if (isfinish) {finish ();} Overridependingtransition (Enteranim, Exitanim);} public void Showstoast (String message) {Toast.maketext (Getapplicationcontext (), message, Toast.length_short). Show (); public void Showltoast (String message{Toast.maketext (Getapplicationcontext (), message, Toast.length_long). Show ();}} 

Test Activity class:

Import Android.text.method.scrollingmovementmethod;import Android.widget.relativelayout;import Android.widget.textview;import Com.example.base.baseactivity;import Com.example.base.oneventtouchfactory.eventtype;public class Mainactivity extends baseactivity {@Overridepublic int Getlayoutresid () {//TODO auto-generated method Stubreturn R.layout.activity_main;} @Overridepublic void Configclass () {//TODO auto-generated method Stubsuper.configclass ();} @Overridepublic void Initialization () {//TODO auto-generated method Stubsuper.initialization (); Relativelayout parent= (relativelayout) Findviewbyid (r.id.parent); TextView child= (TextView) Findviewbyid (r.id.child); Child.setmovementmethod (Scrollingmovementmethod.getinstance ()    ); Parent.setontouchlistener (this);} @Overridepublic void Onright () {//TODO auto-generated method Stubsuper.onright (); Showstoast ("Right");} @Overridepublic void OnTop () {//TODO auto-generated method Stubsuper.ontop (); Showstoast ("Top");} @Overridepublic void Onleft () {//TODO Auto-generated method Stubsuper.onleft (); Showstoast ("Left");} @Overridepublic void Onbottom () {//TODO auto-generated method Stubsuper.onbottom (); Showstoast ("Bottom");} @Overridepublic void Onnortheast () {//TODO auto-generated method Stubsuper.onnortheast (); Showstoast ("Northeast");} @Overridepublic void Onnorthwest () {//TODO auto-generated method Stubsuper.onnorthwest (); Showstoast ("Northwest");} @Overridepublic void Onsoutheast () {//TODO auto-generated method Stubsuper.onsoutheast (); Showstoast ("Southeast");} @Overridepublic void Onsouthwest () {//TODO auto-generated method Stubsuper.onsouthwest (); Showstoast ("Southwest");} @Overridepublic void OnTouch () {//TODO auto-generated method Stubsuper.ontouch ();} @Overridepublic EventType Oneventdistribute () {//TODO auto-generated method Stubreturn Super.oneventdistribute ();}}

The Baseactivity class implements the Oneventlistener and abstracts the class, so a gesture action is required to bind the touch event in the subclass, and gesture manipulation is handled through the Oneventlistener callback function

This is simple gesture manipulation, complex points (such as ontouch conflicts) need to involve classes: View ViewGroup Activity, the method involved: Dispatchtouchevent (event distribution) Onintercepttouchevent ( Event intercept) OnTouch ontouchevent. Activity contains no Onintercepttouchevent method


Android ontouchevent 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.