@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;
}
}
上述代码创建了一个GestureDetector对象,并实现了GestueDetector.OnGestureListener的监听器实例,然后为Activity的Touchevent事件绑定监听器,在事件处理中指定把Activity上的TouchEvent事件交给GestueDetector处理。GestueDetector就会检测是否触发了特定的手势动作。
运行上面的代码,当我们随意在屏幕上触碰时,程序将会检测到我们执行了什么手势。
效果:
下面看一个通过手势缩放图片的例子:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
public class GestureTest extends Activity implements GestureDetector.OnGestureListener
{
//定义手势检测器实例
GestureDetector detector;
ImageView
imageView;
//初始化的图片资源
Bitmap bitmap;
//定义图片的宽高
int width, height;
//记录当前的缩放比
float currentScale = 1;
//控制图片缩放的Matrix对象
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建手势检测器
detector = new GestureDetector(this);
imageView = (ImageView)findViewById(R.id.picture);
matrix = new Matrix();
//获取被缩放的源图片
bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.pic);
//获得位图宽
width = bitmap.getWidth();
//获取位图高
height = bitmap.getHeight();
//设置ImageView初始化时显示的图片
imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources()),R.drawable.pic);
}
//将该Activity上的触碰事件交给GestureDetector处理
@Override
public boolean onTouchEvent(MotionEvent e){
return detector.onTouchEvent(e);
}
@Override
public boolean onDown(MotionEvent e){
Toast.makeText(this, “onDown”, Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
velocityX = velocityX > 4000 ? 4000 : velocityX;
velocityX = velocityX < -4000 ? -4000 : velocityX;
//根据手势的速度来计算缩放比,如果velocityX > 0,放大图像,否则缩小图像
currentScale = currentScale > 0.01 ? currentScale : 0.01f;
//重置Matrix
matrix.reset();
//缩放Matrix
matrix.setScale(currentScale,currentScale,160,200);
BitmapDrawable tmp = (BitmapDrawable)imageView.getDrawable();
//如果图片还未回收,先强制回收该图片
if (!tmp.getBitmap().isRecycled()){
tmp.getBitmap().recycle();
}
//根据原始位图和Matrix创建新图片
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
//显示新的位图
imageView.setImageBitmap(bitmap2);
return true;
}
@Override
public void onLongPress(MotionEvent e){
Toast.makeText(this, “onLongPress”, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
Toast.makeText(this, “onScroll”, Toast.LENGTH_SHORT).show();
return false;
.show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
Toast.makeText(this, “onScroll”, Toast.LENGTH_SHORT).show();
return false;