Android之GestureDetector-简单手势操作及通过手势缩放图片

 Android提供了手势检测,并为其提供了相应的监听器,需要用到的类是GestureDetector,其实例代表了一个手势检测器,创建GestureDetector时需要传入一个GestureDetector.OnGestureListener实例,该实例就是一个监听器,负责对用户的手势行为提供响应。
  GestureDetector.OnGestureListener里包含的事件处理方法如下:

  • boolen onDown(MotionEvent e): 当触碰事件按下时触发该方法。
  • boolean onFling(MotionEvent e, MotionEvent e2, float velocityX, float velocityY): 当用户在屏幕上长按时触发该方法。其中velocityX、velocityY代表“拖过”动作在横向和纵向上的速度。
  • abstract void onLongPress(MotionEvent e): 当用户在屏幕上长按时触发该方法。
  • boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY): 当用户在屏幕上滚动时触发该方法。
  • void onShowPress(MotionEvent e): 当用户在触摸屏上按下,而且还未移动和松开时触发该方法。
  • boolean onSingleTapUp(MotionEvent e): 用户在触摸屏上的轻击事件将会触发该方法。
    下面的例子测试了用户不同的手势操作触发的手势动作:
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class GestureTest extends Activity implements GestureDetector.OnGestureListener
{
    //定义手势检测器实例
    GestureDetector detector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建手势检测器
        detector = new GestureDetector(this);
    }
    //将该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){
        Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
        return  false;
    }
    @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;
    }
    @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;
    }
    @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对象,然后把Activity的OnTouch事件交给GestureDetector处理,但是这个示例实现了 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 方法,在该方法中根据velocityX参数(横向的拖放速度)来计算图片的缩放比,就实现了根据用户手势来缩放图片。

作者:张炳春   原文地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值