前言:android的手势可以通过gestureDetector类完成。
1)通过重写onTouchEvent方法
2)实现OnTouchListener类,重写onTouch方法。
不管哪一种,都需要将touch事件交给gestureDetector来管理。
private GestureDetector gestureDetector; @Override public void onCreate(Bundle savedInstanceState) { gestureDetector = new GestureDetector(this); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); }
现在我们就简单的实现一个图片翻页的效果。
1.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ImageView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" > </ImageView> </LinearLayout>