/**
* 演示【单点触摸手势识别器】
* 演示【缩放手势识别器】最简单的使用
* @author 白乾涛
*/
public class FirstActivity extends Activity implements OnTouchListener {
private ImageView iv;
private GestureDetector mGestureDetector;//单击和双击事件手势识别器
private ScaleGestureDetector mScaleGestureDetector;//缩放事件手势识别器
private Matrix matrix = new Matrix();
private DecimalFormat df = new DecimalFormat("0.00");//格式化float
private float lastScale = 1;//记录上次的缩放比例,下次缩放时是在此基础上进行的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
iv = new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
iv.setScaleType(ScaleType.MATRIX);//用矩阵来绘制
setContentView(iv);
mGestureDetector = new GestureDetector(this, new MyGestureListener());
mScaleGestureDetector = new ScaleGestureDetector(this, new SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = detector.getScaleFactor(); //缩放因子,两指靠拢时小于1
float x = detector.getFocusX(), y = detector.getFocusY();//中心点坐标
Log.i("bqt", "缩放手势 onScale," + df.format(scale) + "-" + df.format(x) + "-" + df.format(y));
matrix.setScale(lastScale * scale, lastScale * scale);
iv.setImageMatrix(matrix);
return super.onScale(detector);
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
Log.i("bqt", "缩放手势 onScaleBegin," + df.format(detector.getScaleFactor()));//始终是1
return super.onScaleBegin(detector);
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
super.onScaleEnd(detector);
lastScale *= detector.getScaleFactor();
Log.i("bqt", "缩放手势 onScaleEnd," + df.format(detector.getScaleFactor()));
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
mScaleGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;//设置OnTouchListener时,返回值要为false,或者在onTouch()中将MotionEvent事件传给GestureDetector,才能使手势识别器生效
}
class MyGestureListener extends SimpleOnGestureListener {
@Override
//双击的【第二下】Touch down时触发(只执行一次)
public boolean onDoubleTap(MotionEvent e) {
Log.i("bqt", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
//双击的【第二下】Touch down和up都会触发(执行次数不确定)。
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i("bqt", "onDoubleTapEvent");
return super.onDoubleTapEvent(e);
}
@Override
//Touch down时触发
public boolean onDown(MotionEvent e) {
Log.i("bqt", "onDown");
return super.onDown(e);
}
@Override
//onScroll一点距离后,【抛掷时】触发(若是轻轻的、慢慢的停止活动,而非抛掷,则很可能不触发)
//参数为手指接触屏幕、离开屏幕一瞬间的动作事件,及手指水平、垂直方向移动的速度,像素/秒
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("bqt", "onFling");
if ((e2.getRawX() - e1.getRawX()) > 100) {
Log.i("bqt", "onFling-从左到右滑");
return true;//消耗事件
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
//Touch了不移动一直Touch down时触发
public void onLongPress(MotionEvent e) {
Log.i("bqt", "onLongPress");
super.onLongPress(e);
}
@Override
//Touch了滑动时触发,e1代表触摸时的事件,是不变的,e2代表滑动过程中的事件,是时刻变化的
//distance是当前event2与上次回调时的event2之间的距离,代表上次回调之后到这次回调之前移动的距离
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i("bqt", "onScroll-" + (int) e1.getX() + "-" + (int) e2.getX() + "-" + (int) distanceX + "-" + (int) distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
//Touch了还没有滑动时触发
public void onShowPress(MotionEvent e) {
Log.i("bqt", "onShowPress");
super.onShowPress(e);
}
@Override
//在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("bqt", "onSingleTapConfirmed");
return super.onSingleTapConfirmed(e);
}
@Override
//在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
public boolean onSingleTapUp(MotionEvent e) {
Log.i("bqt", "onSingleTapUp");
return super.onSingleTapUp(e);
}
}
}