Android App Development Basics-----Gesturedetector (gesture recognition)

Source: Internet
Author: User

Link Address: http://www.cnblogs.com/lknlfy/archive/2012/03/05/2381025.html

I. Overview

Gesturedetector is a class used to identify gestures, where gesture recognition is not identified by the gesture in pattern recognition (the user's hand is used in front of the camera), but by gestures (such as sliding, etc.) made by the user's hand on the touchscreen, which can recognize general gestures, You can also identify user-defined gestures.

Second, the requirements

Use the Gesturedetector, Viewflipper class to switch between two view.

Third, the realization

New Project Mygesture, modify the/res/layout/main.xml file, add a viewflipper inside, complete the Main.xml file as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "vertical" >

7
8 android:id= "@+id/viewflipper"
9 android:layout_width= "Fill_parent"
Ten android:layout_height= "Fill_parent"
>
12
</ViewFlipper>

</LinearLayout>

Next, add 2 layout files under/res/layout: Firstview.xml and Secondview.xml, their contents are a textview and a imageview,firstview.xml are as follows:

1 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
2 android:layout_width= "Fill_parent"
3 android:layout_height= "Fill_parent"
4 android:orientation= "vertical" >
5
6
7 android:layout_width= "Fill_parent"
8 android:layout_height= "Wrap_content"
9 android:text= "This is the first view"
Ten android:textcolor= "#FFFF0000"
android:gravity= "Center_horizontal"
android:textsize= "20dip"
/>
14
15
16
android:id= "@+id/fimg"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:src= "@drawable/sir"
/>
22

</LinearLayout>

Secondview.xml content is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "vertical" >
6
7
8 android:layout_width= "Fill_parent"
9 android:layout_height= "Wrap_content"
Ten android:text= "This is the second view"
android:textcolor= "#FF0000FF"
android:gravity= "Center_horizontal"
android:textsize= "20dip"
/>
15
16
android:id= "@+id/fimg"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:src= "@drawable/android"
/>
22

</LinearLayout>

Finally, modify the Mygestureactivity.java file, the main definition of a Gesturedetectorlistener class for implementing the Gesturedetector.ongesturelistener interface, the complete content is as follows:

1 package com.nan.gesture;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.view.GestureDetector;
6 Import Android.view.LayoutInflater;
7 Import android.view.MotionEvent;
8 Import Android.view.View;
9 Import Android.widget.ViewFlipper;
10
11
12
public class Mygestureactivity extends Activity
14 {
15//Sliding distance difference
private static final int DISTANCE = 40;
17
Private Viewflipper mviewflipper = null;
Private Layoutinflater mlayoutinflater = null;
Private Gesturedetector mgesturedetector = null;
21st
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
25 {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
28
Mviewflipper = (viewflipper) This.findviewbyid (r.id.viewflipper);
Mlayoutinflater = Layoutinflater.from (mygestureactivity.this);
Mgesturedetector = new Gesturedetector (this,new Gesturedetectorlistener ());
32
33//Change layout file Firstview.xml to view object
Firstview View = mlayoutinflater.inflate (R.layout.firstview, NULL);
35//Change layout file Secondview.xml to view object
View Secondview = mlayoutinflater.inflate (R.layout.secondview, NULL);
37//Add View
Mviewflipper.addview (Firstview);
39//Add view
Mviewflipper.addview (Secondview);
41
42}
43
@Override
public boolean ontouchevent (Motionevent event)
46 {
47//Give the touch event to Gesturedetector to handle
Mgesturedetector.ontouchevent return (event);
49}
50
51
public class Gesturedetectorlistener implements Gesturedetector.ongesturelistener
53 {
@Override
public boolean Ondown (Motionevent e)
56 {
//TODO auto-generated method stub
58
59
return false;
61}
62
@Override
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
Velocityy float)
66 {
/TODO auto-generated Method stub
68
if ((E1.getx ()-e2.getx ()) >distance)
70 {
71//Show Next view
Mviewflipper.shownext ();
return true;
74}
All else if ((E2.getx ()-e1.getx ()) >distance)
76 {
77//Show Previous view
Mviewflipper.showprevious ();
return true;
80}
Bayi return false;
82}
83
@Override
onlongpress void (motionevent e)
86 {
/TODO auto-generated Method stub
88
89}
90
@Override
Onscroll Public Boolean (Motionevent E1, motionevent E2,
Distancex float, float distancey)
94 {
Up//TODO auto-generated method stub
--return false;
97}
98
@Override
public void Onshowpress (Motionevent e)
101 {
102//TODO auto-generated method stub
103
104}
105
106 @Override
107 public Boolean onsingletapup (Motionevent e)
108 {
109//TODO auto-generated method stub
return false;
111}
112
113}
114
115}

OK, run the program:

Use your hand to swipe left and right on the screen to try:

It's OK.

The switch between the view here does not add animation effect, if you add animation, you can make the effect of page flipping.

Android App Development Basics-----Gesturedetector (gesture recognition)

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.