Nothing to do, pondering on the android gesture interaction, found that the Internet in the gesture of the article is not many, and a lot of reference value is not big. So out of this blog, and we share. Since I am writing this blog, the study of the interaction of gestures is not very deep, if there is not the correct place, but also asked you to criticize the Bo friends.
First of all, in the Android system, each gesture interaction is executed in the following order.
1. Contact screen Flash, triggering a motionevent event.
2. The event is Ontouchlistener listening and obtains the Motionevent object in its Ontouch () method.
3. Forwards the 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 interaction, together with the following to understand Motionevent, Gesturedetector and Ongesturelistener.
Motionevent: This class is used to encapsulate action events such as gestures, touch pens, trackball, and so on. The interior encapsulates two important properties 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 gesture interaction listening interface, which provides a number of abstract methods, and according to the gesturedetector gesture recognition results call the corresponding method.
Below I again through a toggle beautiful picture (again beautiful picture oh.) Please pay attention to Oh, Pro) code example, demonstrate the implementation of gesture interaction, so that the order of the above, as well as the difference between the gesture action has a more profound understanding and memory.
First, provide a ImageView layout file--main.xml.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "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, to complete our activity, because we want to listen to touch events and gesture time on the touchscreen, it must implement the Ontouchlistener and Ongesturelistener two interfaces and rewrite the methods. The specific code is as follows:
Copy Code code as follows:
public class Mainactivity extends activity implements Ontouchlistener, Ongesturelistener {
Create a Gesturedetector object to identify the clean up
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 to facilitate 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 Girl bar
Image.setimageresource (Girls[index]);
Monitor the touch screen time on this ImageView component
Image.setontouchlistener (this);
Here are two things to remember, or you won't be able to handle events other than light touch, such as throwing motions.
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]);
}
The Ontouch method of rewriting Ontouchlistener
This method is invoked when the touch screen is touched, that is, when a touch event occurs (touching and stroking two events, a 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;
}
Be called when throwing an action
@Override
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
Float velocityy) {
Velocityx represents a lateral movement, switching the girl according to the direction of the finger moving
if (Velocityx < 0) {
GoNext ();
}else if (Velocityx > 0) {
Goprevious ();
}
return false;
}
Be called on a long time
@Override
public void Onlongpress (Motionevent e) {
}
Called when scrolling
@Override
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,
Float Distancey) {
return false;
}
Called while holding
@Override
public void Onshowpress (Motionevent e) {
}
is invoked when lifted
@Override
public boolean onsingletapup (Motionevent e) {
return false;
}
}
When I first started to learn Android, I thought Google's documents were bad, and when I was studying gestures, I felt that Google's documents were really bad. Many constants, properties, and methods are not even described. There is no description, but ongesturelistener gestures so much, it also does not have an introduction to explain, in the absence of continuous before trying, who can understand onlongpress and onshowpress,onscroll and onfling relationship and difference? Google really needs to do a big surgery on the document. But fortunately, after my repeated attempts. Define these gestures from a personal point of view.
Press (Ondown): The moment when the finger touches the touch screen, is the touch of that.
Toss (onfling): The finger moves quickly on the touch screen and loosens.
Long Press (onlongpress): The finger presses for a period of time and does not loosen.
Scrolling (onscroll): The finger slides on the touch screen.
Press and Hold (onshowpress): The finger presses on the touch screen, its time range is pressed to work, 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 as a lesson, here to share with you.
Any gesture action will perform a press (Ondown) action first.
You must perform a hold (onshowpress) action before the Long Press (onlongpress) action.
Hold (onshowpress) action and press (Ondown) action to perform a lift (onsingletapup) action.
The lift (Onsingletapup) action is not performed after the long Press (onlongpress), scrolling (onscroll), and throwing (onfling) actions.
Speaking of which, it is almost over. The rest is to see the results of the operation together.
one, not thrown:
Two, toss to the right once
Three, right and throw again.