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
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:o Rientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" >
- <ImageView android:id="@+id/image" android:layout_width="Fill_parent" android: layout_height= "fill_parent" android:layout_gravity= "Center"/>
- </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
- Public class Mainactivity extends Activity implements Ontouchlistener, Ongesturelistener {
- //Create a Gesturedetector object to identify the pack waiyuwu.blogcn.com
- private Gesturedetector detector = new Gesturedetector (this);
- //Define an array to put the beautiful girl
- int[] Girls = new int[]{r.drawable.girl1, R.drawable.girl2, r.drawable.girl3};
- //define array subscript for easy viewing of each girl
- private int index;
- private ImageView image;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Image = (ImageView) Findviewbyid (r.id.image);
- //Set an initial display of the girl bar
- Image.setimageresource (Girls[index]);
- //monitor touch screen time on this ImageView component
- Image.setontouchlistener (this);
- /////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.
- Image.setlongclickable (true);
- Detector.setislongpressenabled (true);
- }
- //A way to shout the next girl
- public void GoNext () {
- index++;
- index = math.abs (index% girls.length);
- Image.setimageresource (Girls[index]);
- }
- //The way users call on a girl
- public void Goprevious () {
- index--;
- index = math.abs (index% girls.length);
- Image.setimageresource (Girls[index]);
- }
- //Rewrite the Ontouch method of Ontouchlistener
- //This method is called when touch screen is touched, that is, touch events (touching and stroking two events, pretty image).
- @Override
- Public Boolean OnTouch (View V, motionevent event) {
- Detector.ontouchevent (event);
- return true;
- }
- //Called when the action is pressed
- @Override
- Public Boolean ondown (Motionevent e) {
- return false;
- }
- //is called when throwing an action
- @Override
- Public Boolean onfling (motionevent E1, motionevent E2, float Velocityx,
- float Velocityy) {
- //velocityx indicates lateral movement, switching girls according to the direction of the finger movement
- if (Velocityx < 0) {
- GoNext ();
- }Else if (Velocityx > 0) {
- Goprevious ();
- }
- return false;
- }
- //be called on long and on time
- @Override
- public void Onlongpress (Motionevent e) {
- }
- //Call when scrolling
- @Override
- Public Boolean onscroll (motionevent E1, motionevent E2, float Distancex,
- float Distancey) {
- return false;
- }
- //is called when pressed
- @Override
- public void Onshowpress (Motionevent e) {
- }
- //is called when lifted
- @Override
- Public Boolean onsingletapup (Motionevent e) {
- return false;
- }
- }
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]