本文参考文献:《疯狂Android讲义 : 第2版 》
使用 Android 手势监测需要两个步骤:
- 实现 GestureDetector.OnGestureListener 监听器实例以创建 GestureDetector 对象;
- 为组件的 TouchEvent 事件绑定监听器,在事件处理中把组件上的 TouchEvent 事件交给 GestureDetector 处理,而 GestureDetector 就会检测是否触发了特定的手势动作。
下面是测试不同动作触发哪种手势的实例:
package com.toby.personal.testlistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
final private static String TAG = "Toby_Test";
private GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 必须创建手势检测器
detector = new GestureDetector(this, this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 注意此处的处理
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
return false;
}
}
通过 Fling 手势缩放图片
主布局文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:background="@color/colorGray"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
主程序的内容如下:
package com.toby.personal.testlistview;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
final private static String TAG = "Toby_Test";
private GestureDetector detector;
private ImageView imageView;
private Bitmap bitmap;
private int width;
private int height;
private float currentScale = 1;
private Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 必须创建手势检测器
detector = new GestureDetector(this, this);
imageView = (ImageView) findViewById(R.id.image);
matrix = new Matrix();
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl04);
width = bitmap.getWidth();
height = bitmap.getHeight();
imageView.setImageBitmap(
BitmapFactory.decodeResource(this.getResources(), R.drawable.girl04));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 注意此处的处理
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
velocityX = Math.max(Math.min(velocityX, 4000), -4000);
currentScale += currentScale * velocityX / 4000.0f;
currentScale = Math.max(currentScale, 0.01f);
matrix.reset();
matrix.setScale(currentScale, currentScale, 160, 200);
BitmapDrawable tmp = (BitmapDrawable) imageView.getDrawable();
if (!tmp.getBitmap().isRecycled()) { // 如果图片还未回收,先强制回收该图片
tmp.getBitmap().recycle();
}
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
imageView.setImageBitmap(bitmap2);
return true;
}
}
通过手势实现翻页效果
主布局文件的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:background="@color/colorGray"
android:orientation="vertical"
>
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flipper"
/>
</LinearLayout>
主程序文件的内容如下:
package com.toby.personal.testlistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
final private static String TAG = "Toby_Test";
private ViewFlipper flipper;
private GestureDetector detector;
Animation[] animations = new Animation[4];
final int FLIP_DISTANCE = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 必须创建手势检测器
detector = new GestureDetector(this, this);
flipper = (ViewFlipper) findViewById(R.id.flipper);
flipper.addView(addImageView(R.drawable.girl01));
flipper.addView(addImageView(R.drawable.girl02));
flipper.addView(addImageView(R.drawable.girl03));
flipper.addView(addImageView(R.drawable.girl04));
flipper.addView(addImageView(R.drawable.girl05));
animations[0] = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
animations[1] = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);
}
private View addImageView(int resId) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(resId);
imageView.setScaleType(ImageView.ScaleType.CENTER);
return imageView;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 注意此处的处理
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
flipper.setInAnimation(animations[0]);
flipper.setOutAnimation(animations[1]);
flipper.showPrevious();
return true;
}
if (e2.getX() - e1.getX() > FLIP_DISTANCE){
flipper.setInAnimation(this, android.R.anim.slide_in_left);
flipper.setOutAnimation(this, android.R.anim.slide_out_right);
flipper.showNext();
return true;
}
return false;
}
}