Android Learning Guide 38: Android gesture manipulation programming [go]

Source: Internet
Author: User

Gesture manipulation offers a different experience in the process of using smart devices. Programming for gesture manipulation is inevitable in Android development. So what is the principle of it? How do we program with gesture manipulation?

gesture Operation Principle

First, in an Android system, each gesture interaction is performed in the following order.

1. Touch the touch screen flash, triggering a motionevent event.

2. The event is ontouchlistener monitored to obtain the Motionevent object in its Ontouch () method.

3. Forward the secondary Motionevent object to Ongesturelistener via Gesturedetector (gesture recognizer).

4. Ongesturelistener obtains the object and listens to the information encapsulated by the object to make appropriate feedback.

This order can be said to be the principle of gesture manipulation.

gesture manipulation classes and interfaces

Here's a look at Motionevent, Gesturedetector and Ongesturelistener.

Motionevent: This class is used to encapsulate action events such as gestures, touch pens, trackball, and so on. It internally encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes, respectively.

Gesturedetector: Identify various gestures.

Ongesturelistener: This is a listener interface for gesture interaction, which provides several abstract methods, and calls the corresponding method according to the gesturedetector gesture recognition result.

Gesture Manipulation Examples

Here I go through a code example to switch beautiful pictures, demonstrate the implementation of the gesture interaction, so that the order of execution of the above, as well as the various gestures of the distinction between a more profound understanding and memory.

First, provide a layout file that has only ImageView--main.xml.

xml/html Code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"  android:o Rientation= "vertical"  android:layout_width= "fill_parent"  android:layout_height= "fill_parent" >    
  3. <ImageView android:id="@+id/image" android:layout_width="Fill_parent" android: layout_height= "fill_parent" android:layout_gravity= "Center"/>
  4. </linearlayout>

Then, complete our activity, because to listen to touch screen touch events and gesture time, so the activity must implement Ontouchlistener and Ongesturelistener two interfaces, and override the methods. The specific code is as follows:

Java code
  1. Public class Mainactivity extends Activity implements Ontouchlistener, Ongesturelistener {
  2. //Create a Gesturedetector object to identify the pack waiyuwu.blogcn.com
  3. private Gesturedetector detector = new Gesturedetector (this);
  4. //Define an array to put the beautiful girl
  5. int[] Girls = new int[]{r.drawable.girl1, R.drawable.girl2, r.drawable.girl3};
  6. //define array subscript for easy viewing of each girl
  7. private int index;
  8. private ImageView image;
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. Image = (ImageView) Findviewbyid (r.id.image);
  14. //Set an initial display of the girl bar
  15. Image.setimageresource (Girls[index]);
  16. //monitor touch screen time on this ImageView component
  17. Image.setontouchlistener (this);
  18. /////below two to remember to set Oh, otherwise you will not be able to deal with events other than the light touch, such as throwing action.
  19. Image.setlongclickable (true);
  20. Detector.setislongpressenabled (true);
  21. }
  22. //A way to shout the next girl
  23. public void GoNext () {
  24. index++;
  25. index = math.abs (index% girls.length);
  26. Image.setimageresource (Girls[index]);
  27. }
  28. //The way users call on a girl
  29. public void Goprevious () {
  30. index--;
  31. index = math.abs (index% girls.length);
  32. Image.setimageresource (Girls[index]);
  33. }
  34. //Rewrite the Ontouch method of Ontouchlistener
  35. //This method is called when touch screen is touched, that is, touch events (touching and stroking two events, pretty image).
  36. @Override
  37. Public Boolean OnTouch (View V, motionevent event) {
  38. Detector.ontouchevent (event);
  39. return true;
  40. }
  41. //Called when the action is pressed
  42. @Override
  43. Public Boolean ondown (Motionevent e) {
  44. return false;
  45. }
  46. //is called when throwing an action
  47. @Override
  48. Public Boolean onfling (motionevent E1, motionevent E2, float Velocityx,
  49. float Velocityy) {    
  50. //velocityx indicates lateral movement, switching girls according to the direction of the finger movement
  51. if (Velocityx < 0) {
  52. GoNext ();
  53. }Else if (Velocityx > 0) {
  54. Goprevious ();
  55. }
  56. return false;
  57. }
  58. //be called on long and on time
  59. @Override
  60. public void Onlongpress (Motionevent e) {
  61. }
  62. //Call when scrolling
  63. @Override
  64. Public Boolean onscroll (motionevent E1, motionevent E2, float Distancex,
  65. float Distancey) {    
  66. return false;
  67. }
  68. //is called when pressed
  69. @Override
  70. public void Onshowpress (Motionevent e) {
  71. }
  72. //is called when lifted
  73. @Override
  74. Public Boolean onsingletapup (Motionevent e) {
  75. return false;
  76. }
  77. }

the meaning of each method of gesture manipulation

When I first started learning Android, I felt that Google's documents were not good, and when we studied gestures, I felt that Google's documents were too poor. A lot of constants, properties, and methods don't even have a description. There is no description, but there are so many gestures in the ongesturelistener, it does not have a description, who can understand the relationship and difference between onlongpress and Onshowpress,onscroll and onfling before attempting to do it continuously? Google really needs to do a big surgery on the documentation. But fortunately, after my repeated attempts. These gestures are defined from a personal point of view.

Press (Ondown): Just the moment the finger touches the touch screen, it is the touch of the moment.
Throw (onfling): The finger moves quickly on the touchscreen and releases the action.
Long Press (onlongpress): The finger is pressed for a period of time and is not loosened.
Scrolling (onscroll): Fingers slide on the touchscreen.
Press and Hold (onshowpress): The finger is pressed on the touchscreen, its time range is pressed, and before the long press.
Lift (Onsingletapup): The moment the finger leaves the touch screen.
In addition to these definitions, I also summed up a bit of experience, and here to share with you.

Any gesture action will first perform a pressing (Ondown) action.
Press and hold (onshowpress) action before long pressing (onlongpress) action.
A lift (Onsingletapup) action is performed after holding (onshowpress) the action and pressing (Ondown) the action.
Hold (onlongpress), scroll (onscroll), and throw (onfling) actions do not perform a lift (onsingletapup) action.

From:http://www.jizhuomi.com/android/course/264.html

Android Learning Guide 38: Android gesture manipulation programming [go]

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.