今天和大家来谈一下GestureDetector(手势),首先来看一下效果(是一个单指缩放效果)
由于在模拟器不能触摸,所以效果有点丑陋,大家就将就着点看吧
在Android中,为手势检测提供了一个GestUreDetector类,GestureDetector实例代表了一个手势检测器,创建GestureDetector时需要传入一个GestureDetector.OnGestureListener实例,GestureDetector.OnGestureListener就是一个监听器,负责对用户的手势行为提供响应。
GestureDetector.OnGestureListene里包含的事件处理方法如下。
(1)boolean onDown(MotionEvent e ):当触摸事件按下时触发该方法。
(2)boolean onFliing(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY):
当用户在触摸屏上“拖过”时触发该方法。其中velocityX,velocityY代表“拖过”动作在横向,纵向上的速度。
(3)abstract void onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法
(4)boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY):当用户在屏幕上“滚动”时触发该方法
(5)void onShowPress(MotionEvent e):当用户在触摸屏上按下,而且还未移动和松开时触发该方法。
(6)boolean onSingleTagUp(MotionEvent e):用户在触摸屏上的轻击事件将会触发该方法。
关于GestureDetector.OnGestureListene监听器里各个方法的触发时机,仅从文字上表述总显得比较抽象且难以理解,建议大家写个小例子,在各个复写的方法中写个Toast试下,我在这边就不写例子了,大家可以试下
使用手势检测只需三个步骤:
(1)创建一个GestureDetector对象。创建该对象时必须实现一个GestureDetector.OnGestureListene监听器实例。
(2)为应用程序的Activity(偶尔也可为特定组件)的TouchEvent事件绑定监听器,在事件处理中指定把Activity(或特定组件)上的TouchEvent事件交给GestureDetector处理
(3)在继承GestureDetector.OnGestureListener的各个复写的方法中,根据需求自己编写
OK,基本概念就说到这,下面我们的例子是通过手势缩放图片,图片缩放技术的实现,通过Matrix(矩阵,不了解的可以上网搜资料,网上有很多哦)即可以实现,平时的图片缩放要么用按钮控制,要么通过SeekBar来控制图片缩放,这些都比较传统,本示例所实现的则炫的多:用户只要在图片上随意的“挥动”手指,图片就可以被缩放——从左到右挥动被放大,当从右到左挥动是图片被缩小,挥动速度越快,缩放比越大
该实例的布局较简单,就一个ImageView,该实例的思路使用一个GestureDetector来检测用户的手势,并根据用户手势在横向的速度缩放图片
示例代码:
package com.example.gesture;
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.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
public class GestureTestActivityActivity extends Activity implements
OnGestureListener {
//定义手势检测器实例
private GestureDetector detector;
private ImageView imageView;
//初始的图片资源
private Bitmap bitmap;
//定义图片的宽高
private int width, height;
//记录当前的缩放比
private float currentScale = 1;
//控制图片缩放的Matrix对象
private Matrix matrix;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建手势检测器
detector = new GestureDetector(this);
imageView = (ImageView) findViewById(R.id.img);
matrix = new Matrix();
//获取被缩放的源图片
bitmap = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher);
//获得位图宽
width = bitmap.getWidth();
//获得位图高
height = bitmap.getHeight();
//获得imageView初始化的显示的图片
imageView.setImageBitmap(BitmapFactory.decodeResource(
this.getResources(), R.drawable.ic_launcher));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//将该Activity上的触碰事件交给gestUreDetector处理
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// Toast.makeText(this, "OnDown____________", 8000).show();
return false;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
// Toast.makeText(this, "onFling____________", 8000).show();
velocityX = velocityX > 4000 ? 4000 : velocityX;
velocityX = velocityX < -4000 ? -4000 : velocityX;
//根据手势的速度来计算缩放比,如果velocityX > 0,放大图片,否则缩小
currentScale += currentScale * velocityX / 4000.0f;
//保证currentScale不会等于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);
// 改为true,事件向下传递
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// Toast.makeText(this, "onLongPress____________", 8000).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// Toast.makeText(this, "onScroll____________", 8000).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// Toast.makeText(this, "onShowPress____________", 8000).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Toast.makeText(this, "onSingleTapUp____________", 8000).show();
return false;
}
}
Ok,运行的效果就如上面所示,例子比较简单,只实现了单指缩放,大家可以试下