Android Touch Events and sample code _android

Source: Internet
Author: User
Tags abs gety

Events triggered by touching (touch)

Android events: OnClick, onscroll,onfling, etc., are made up of many touch. The first state of touch is definitely action_down, which means that the screen is pressed. After that, the touch will have a follow-up event, which may be:

Action_move//Express as mobile gesture

ACTION_UP//represented as leaving screen

Action_cancel//means cancellation gesture, not generated by the user, but by the program

A action_down, N-action_move, 1-action_up, constitute a number of events on Android.

An important way to control a ViewGroup class is onintercepttouchevent (), which handles the event and changes the direction in which the event is passed, and its return value is a Boolean value that determines whether the touch event will continue to pass to the child view it contains. This method is passed from the parent view to the child view.

The method Ontouchevent () is used to receive events and processes, and its return value is also a Boolean value that determines whether the event and subsequent events continue to pass upward, which is passed from the child view to the parent view.

The transfer mechanism between the touch events in Onintercepttouchevent () and ontouchevent and each childview depends entirely on the return value of Onintercepttouchevent () and Ontouchevent (). A return value of TRUE indicates that the event was received and processed correctly, and the return value of FALSE indicates that the event was not processed and continues to pass.

The Action_down event passes to the onintercepttouchevent of a ViewGroup class, and if False is returned, the down event continues to be passed to the onintercepttouchevent of the child ViewGroup class, If the child view is not a control of the ViewGroup class, it is passed to its ontouchevent.

If Onintercepttouchevent returns True, the down event is passed to its ontouchevent and no longer passes, and subsequent events are passed on to its ontouchevent.

If the ontouchevent of a view returns false, the down event continues to be passed to the ontouchevent of its parent ViewGroup class, and if true is returned, subsequent events are passed directly to its ontouchevent to continue processing. (subsequent events are passed only to ontouchevent that returns true for the necessary events Action_down)

To sum up: Onintercepttouchevent can accept all the touch events, while ontouchevent is not necessarily.

For Android custom control events Android provides a Gesturedetector class and Gesturedetector.ongesturelistener interface to determine what kind of action the user is making on the interface.

There are two classes in Android

Android.view.GestureDetector
Android.view.GestureDetector.SimpleOnGestureListener
(in addition, Android.widget.Gallery seems to be more ongesturelistener of the Ox x)

  1 Create a new class inheritance Simpleongesturelistener,hahagesturedetectorlistener

The following event events can be implemented.
Boolean Ondoubletap (Motionevent e)
Explanation: Double click on the second touch down when the trigger
Boolean ondoubletapevent (Motionevent e)
Explanation: Double click down and up will trigger, and e.getaction () can be used to distinguish between the two.
Boolean Ondown (Motionevent e)
Explanation: Trigger when touching down
Boolean onfling (Motionevent E1, motionevent E2, float velocityx,float velocityy)
Explanation: Touch it by sliding a little distance after the up is triggered.
void Onlongpress (Motionevent e)
Explanation: The touch is not moving, always touching down.
Boolean onscroll (Motionevent E1, motionevent E2, float distancex,float Distancey)
Explanation: Touch the trigger when sliding.
void Onshowpress (Motionevent e)
Explanation: Touch has not yet slipped when triggered
(Compared with ondown,onlongpress
Ondown as soon as touch down must trigger immediately.
and touchdown after a while not sliding first trigger onshowpress again is onlongpress.
So touchdown does not slide, ondown->onshowpress->onlongpress this sequence trigger.

Boolean onsingletapconfirmed (Motionevent e)
Boolean onsingletapup (Motionevent e)
Explanation: Both of the above functions are touchdown and not sliding (onscroll), without a long press (onlongpress), and then triggered when touchup.
Click on the very fast (do not slide) Touchup:
Ondown->onsingletapup->onsingletapconfirmed
Click a little slower (do not slide) Touchup:
Ondown->onshowpress->onsingletapup->onsingletapconfirmed

Publicclassgestureactivityextendsactivityimplementsontouchlistener, Ongesturelistener {GestureDetector detector;
  Publicgestureactivity () {detector = new Gesturedetector (this); 
      } publicvoidoncreate (Bundlesavedinstancestate) {super.oncreate (savedinstancestate); 
      Setcontentview (R.layout.main);
      TextView TV = (TextView) Findviewbyid (r.id.textview001); 
      Set the TV listener Tv.setontouchlistener (this);
      Tv.setfocusable (TRUE); 
      The view must be able to handle hold tv.setclickable (true) different from tap (touch); 
      Tv.setlongclickable (TRUE); 
  Detector.setislongpressenabled (TRUE); 
  Publicbooleanontouch (View v,motionevent event) {returndetector.ontouchevent (event); 
    }//user Touch touch screen, triggered by 1 Motioneventaction_down publicbooleanondown (MotionEventarg0) {log.i ("mygesture", "Ondown"); 
    Toast.maketext (This, "Ondown", Toast.length_short). Show (); 
  Returntrue; } publicvoidonshowpress (Motionevent e) {log.i ("MYgesture "," onshowpress "); 
  Toast.maketext (This, "onshowpress", Toast.length_short). Show (); }//The user (after touching the touch screen) released, triggered by a 1 motioneventaction_up publicbooleanonsingletapup (motionevent e) {log.i ("mygesture" 
    , "Onsingletapup"); 
    Toast.maketext (This, "Onsingletapup", Toast.length_short). Show (); 
  Returntrue;  }//The user presses the touch screen, quickly move after release, by 1 Motioneventaction_down, multiple action_move, 1 action_up Trigger publicbooleanonfling (MotionEvente1, 
    
    Motionevent E2, float Velocityx, float velocityy) {log.i ("mygesture", "onfling"); Parameter explanation://E1:1th action_downmotionevent//E2: Moving speed on last action_movemotionevent//velocityx:x axis, pixel/sec/ 
     
    /Velocityy:y axis movement speed, pixel/sec/s Trigger condition://x axis coordinate displacement greater than fling_min_distance, and move speed is greater than fling_min_velocity pixel/sec 
    Finalintfling_min_distance = 100,fling_min_velocity = 200; if (E1.getx ()-E2.getx () >fling_min_distance &&math.abs (Velocityx) > Fling_min_velocity) {//Flingle FT log.i ("MygeSture "," Fling left "); 
    Toast.maketext (This, "Flingleft", Toast.length_short). Show (); } elseif (E2.getx ()-E1.getx () >fling_min_distance &&math.abs (Velocityx) > Fling_min_velocity) {//F 
      Lingright log.i ("Mygesture", "Fling Right"); 
    Toast.maketext (This, "Flingright", Toast.length_short). Show (); } elseif (E2.gety ()-e1.gety () >fling_min_distance && math.abs (velocityy) >fling_min_velocity) {//Flingd 
      Own log.i ("Mygesture", "Fling Down");
    Toast.maketext (This, "Flingdown", Toast.length_short). Show (); } elseif (E1.gety ()-e2.gety () >fling_min_distance && math.abs (velocityy) >fling_min_velocity) {//FLING 
      Up LOG.I ("Mygesture", "Fling up");
    Toast.maketext (This, "Flingup", Toast.length_short). Show ();
    
  } Returnfalse;  }//The user presses the touchscreen and drags it by 1 motioneventaction_down, multiple action_move triggering publicbooleanonscroll (MotionEvente1, motionevent E2, Float Distancex, Float distAncey) {log.i ("mygesture", "onscroll"); 
    Toast.maketext (This, "onscroll", Toast.length_long). Show (); 
  Returntrue; }//user long touch screen, triggered by multiple Motioneventaction_down publicvoidonlongpress (motionevent e) {log.i ("mygesture", "ONLONGPR 
    ESS "); 
  Toast.maketext (This, "onlongpress", Toast.length_long). Show ();

 } 
  

}

2 A new Gesturedetector object in view.

In the constructor function

Gesturedetector = new Gesturedetector (Newhahagesturedetectorlistener ());

Then you can write your own code in the ontouchevent of the view by using the following in the event you just made in 1.

  @Override public boolean ontouchevent (Motionevent event) {gesturedetector.ontouchevent (event); Mtouchlistener = new Ontouchlistener () {@Override public boolean ontouch (View V, motionevent event) {//TOD
   O auto-generated Method Stub float x =event.getxprecision () *event.getx () +event.getx ();
   Float y =event.getyprecision () *event.gety () +event.gety ();
   Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:break;
   Case motionevent.action_move:mtouchtimes++; if (Mtouchtimes > Touch_times) {//according to direction calculation angle if (mcurrentorientation==deviceorientation.landscape) {mangle = M
   Ath.todegrees (Math.atan2 (Y-480/2, x)) +90;
   else {mangle =-math.todegrees (math.atan2 (y-480/2,320-x)) +90;
   } LOG.W ("angle", "mangle:" +mangle);
   } break;
   Case MotionEvent.ACTION_UP:if (Mtouchtimes > Touch_times) {} else {} mtouchtimes = 0;
   Break
  Default:break;
   return true;
  }    }; Mview.setOntouchlistener (Mtouchlistener);
  

Through this article, I hope to help develop Android applications using touch events friends, thank you for your support to this site!

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.