Android手势识别类 GestureDetector

jopen 10年前

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:

boolean  onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean  onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean  onDown(MotionEvent e)
解释:Touch down时触发
boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void  onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void  onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较
onDown只要Touch down一定立刻触发。
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。
boolean  onSingleTapConfirmed(MotionEvent e)
boolean  onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

 

 

用一个图片切换的例子来说明手势识别类的使用。

java代码:

</div> </div>
    package com.lovo;                import android.app.Activity;        import android.os.Bundle;        import android.view.GestureDetector;        import android.view.GestureDetector.OnGestureListener;        import android.view.MotionEvent;        import android.widget.ImageView;        /**         * 手势识别类         * 图片切换显示,实现接口OnGestureListener         * @author Administrator         *         */        public class MyGestrueTest extends Activity implements OnGestureListener{            /**手势管理器*/            private GestureDetector detector;            /**图片资源数组*/            private int [] imageId;                        /**图片视图*/            private ImageView imageView ;            private ImageView[] imgView;                    /**图片下标计数器*/            private int count=0;                        @Override            protected void onCreate(Bundle savedInstanceState) {                        super.onCreate(savedInstanceState);                setContentView(R.layout.my_gesture_test);                //创建手势管理器                detector = new GestureDetector(this);                //得到图片视图                imageView=(ImageView) findViewById(R.id.image_gestrue);                                //得到图片资源的Id                imageId = new int[]{                        R.drawable.whatsnew_fornew_01,                        R.drawable.whatsnew_fornew_02,                        R.drawable.whatsnew_fornew_03,                        R.drawable.whatsnew_fornew_04,                        R.drawable.whatsnew_fornew_05};                //得到图片视图 ImageView                 imgView = new ImageView []{                        (ImageView) findViewById(R.id.small_image1),                        (ImageView) findViewById(R.id.small_image2),                        (ImageView) findViewById(R.id.small_image3),                        (ImageView) findViewById(R.id.small_image4),                        (ImageView) findViewById(R.id.small_image5)};                            }            @Override            public boolean onTouchEvent(MotionEvent event) {                //将touch事件交给手势管理器来处理                return detector.onTouchEvent(event);            }            @Override            public boolean onDown(MotionEvent e) {                return false;            }            /**             * 手势滑动触发的事件             */            @Override            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                    float velocityY) {                //从右往左移动                if(e1.getX()-e2.getX()>50){                    //下标++                              count++;                                if(count<=4){                        //切换到下一张                        imageView.setImageResource(imageId[count]);                        //设置该张对应的高亮小图标                        imgView[count].setImageResource(R.drawable.page_indicator_focused);                        //其他小图标均为非高亮                        imgView[count-1].setImageResource(R.drawable.page_indicator_unfocused);                    }else{                        //滑动到最后一张,该方向不会再滑动,就停留在最后一张                        count=4;                        imageView.setImageResource(imageId[count]);                                 }                }                //从左往右移动                if(e1.getX()-e2.getX()<50){                            count--;                                        if(count>=0){                        imageView.setImageResource(imageId[count]);                        imgView[count].setImageResource(R.drawable.page_indicator_focused);                        imgView[count+1].setImageResource(R.drawable.page_indicator_unfocused);                    }else{                        count=0;                        imageView.setImageResource(imageId[count]);                    }                }                return false;            }                    @Override            public void onLongPress(MotionEvent e) {}            @Override            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,                    float distanceY) {                return false;            }            @Override            public void onShowPress(MotionEvent e) {}            @Override            public boolean onSingleTapUp(MotionEvent e) {                return false;            }        }  


 

XML:

<?xml version="1.0" encoding="utf-8"?>    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"         android:background="#000">        <LinearLayout            android:id="@+id/my_geture_test_1"            android:layout_width="match_parent"            android:layout_height="match_parent" >                <ImageView                android:id="@+id/image_gestrue"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:src="@drawable/whatsnew_fornew_01" >            </ImageView>        </LinearLayout>            <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"                  android:gravity="center_horizontal"             android:layout_marginBottom="30dp"               >            <ImageView                android:id="@+id/small_image1"                android:padding="5dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/page_indicator_focused"                 >            </ImageView>                <ImageView                android:id="@+id/small_image2"                android:padding="5dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/page_indicator_unfocused" >            </ImageView>                <ImageView                android:id="@+id/small_image3"                android:padding="5dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/page_indicator_unfocused" >            </ImageView>                <ImageView                android:id="@+id/small_image4"                android:padding="5dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/page_indicator_unfocused" >            </ImageView>            <ImageView                android:id="@+id/small_image5"                android:padding="5dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/page_indicator_unfocused" >            </ImageView>        </LinearLayout>    </RelativeLayout>  
</div> </div>