Android應用開發基礎篇(13)-----GestureDetector(手勢識別)

來源:互聯網
上載者:User

標籤:

連結地址:http://www.cnblogs.com/lknlfy/archive/2012/03/05/2381025.html

一、概述

      GestureDetector是一個用於識別手勢的類,這裡所講的手勢識別,不是模式識別裡所講的手勢(使用者的手在用網路攝影機前做的動作)識別,而是說使用者的手在觸控螢幕上做的手勢(比如滑動等),它可以識別一般的手勢,也可以識別使用者自訂的手勢。

 

二、要求

     利用GestureDetector、ViewFlipper類實現兩個View之間的切換。

 

三、實現

     建立工程MyGesture,修改/res/layout/main.xml檔案,在裡面添加一個ViewFlipper,完整的main.xml檔案如下:

 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 <ViewFlipper
8 android:id="@+id/viewflipper"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 >
12
13 </ViewFlipper>
14
15 </LinearLayout>

接著,在/res/layout下添加2個布局檔案:firstview.xml和secondview.xml,它們的內容都是一個TextView和一個ImageView,firstview.xml如下:

 

 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 <TextView
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:text="這是第一個View"
10 android:textColor="#FFFF0000"
11 android:gravity="center_horizontal"
12 android:textSize="20dip"
13 />
14
15
16 <ImageView
17 android:id="@+id/fimg"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:src="@drawable/sir"
21 />
22
23
24 </LinearLayout>

secondview.xml內容如下:

 

 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 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="這是第二個View"
11 android:textColor="#FF0000FF"
12 android:gravity="center_horizontal"
13 android:textSize="20dip"
14 />
15
16 <ImageView
17 android:id="@+id/fimg"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:src="@drawable/android"
21 />
22
23
24 </LinearLayout>

最後,修改MyGestureActivity.java檔案,主要定義一個GestureDetectorListener類用於實現GestureDetector.OnGestureListener介面,完整的內容如下:

 

  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
13 public class MyGestureActivity extends Activity
14 {
15 //滑動距離的差值
16 private static final int DISTANCE = 40;
17
18 private ViewFlipper mViewFlipper = null;
19 private LayoutInflater mLayoutInflater = null;
20 private GestureDetector mGestureDetector = null;
21
22 /** Called when the activity is first created. */
23 @Override
24 public void onCreate(Bundle savedInstanceState)
25 {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28
29 mViewFlipper = (ViewFlipper)this.findViewById(R.id.viewflipper);
30 mLayoutInflater = LayoutInflater.from(MyGestureActivity.this);
31 mGestureDetector = new GestureDetector(this,new GestureDetectorListener());
32
33 //將布局檔案firstview.xml變為View對象
34 View firstView = mLayoutInflater.inflate(R.layout.firstview, null);
35 //將布局檔案secondview.xml變為View對象
36 View secondView = mLayoutInflater.inflate(R.layout.secondview, null);
37 //添加View
38 mViewFlipper.addView(firstView);
39 //添加View
40 mViewFlipper.addView(secondView);
41
42 }
43
44 @Override
45 public boolean onTouchEvent(MotionEvent event)
46 {
47 //將觸摸事件交給GestureDetector來處理
48 return mGestureDetector.onTouchEvent(event);
49 }
50
51
52 public class GestureDetectorListener implements GestureDetector.OnGestureListener
53 {
54 @Override
55 public boolean onDown(MotionEvent e)
56 {
57 // TODO Auto-generated method stub
58
59
60 return false;
61 }
62
63 @Override
64 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
65 float velocityY)
66 {
67 // TODO Auto-generated method stub
68
69 if((e1.getX()-e2.getX())>DISTANCE)
70 {
71 //顯示下一個view
72 mViewFlipper.showNext();
73 return true;
74 }
75 else if((e2.getX()-e1.getX())>DISTANCE)
76 {
77 //顯示前一個view
78 mViewFlipper.showPrevious();
79 return true;
80 }
81 return false;
82 }
83
84 @Override
85 public void onLongPress(MotionEvent e)
86 {
87 // TODO Auto-generated method stub
88
89 }
90
91 @Override
92 public boolean onScroll(MotionEvent e1, MotionEvent e2,
93 float distanceX, float distanceY)
94 {
95 // TODO Auto-generated method stub
96 return false;
97 }
98
99 @Override
100 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
110 return false;
111 }
112
113 }
114
115 }

好了,運行該程式:

 

用手在螢幕上左右滑動試下:

 

OK了。

     這裡view之間的切換並沒有加上動畫效果,如果加上動畫的話就可以做出翻頁的效果。

Android應用開發基礎篇(13)-----GestureDetector(手勢識別)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.