【安卓笔记】GestureDetector浅析

GestureDetector类用于检测一系列由 MotionEvent提供的手势事件,当特定的手势事件发生时,通过调用
GestureDetector.OnGestureDetectorListener接口中定义的回调函数通知用户。
使用方式:
1.为你的view创建一个GestureDetector的实例;
2.复写view的onTouchEvent,并调用GestureDetector的OnTouchEvent方法。

使用示例:
package com.example.gesturedetectordemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;

/**
 * @author Rowand jj
 *
 *GestureDetector的使用步骤:
 *    1.创建GestureDetector实例
 *    2.复写view类的onTouchEvent,调用GestureDetector的onTouchEvent
 */
public class MainActivity extends Activity implements OnGestureListener
{
    private static final String TAG = "MainActivity";
    private GestureDetector mGestureDector = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mGestureDector = new GestureDetector(this,this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        //监听手势事件
        return mGestureDector.onTouchEvent(event);
    }
    @Override
    public boolean onDown(MotionEvent e)
    {
        Log.i(TAG,"onDown run..");
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
        Log.i(TAG,"onShowPress run..");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        Log.i(TAG,"onSingleTapUp run..");
        return false;
    }
    @Override// 用户按下触摸屏,并拖动
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY)
    {
        Log.i(TAG,"onScroll run..");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
        Log.i(TAG,"onLongPress run..");        
    }
    //用户按下触摸屏、快速移动后松开
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY)
    {
        Log.i(TAG,"onFling run..");
        return false;
    }

}
测试:
点击屏幕:
【安卓笔记】GestureDetector浅析
点击屏幕不松手:
【安卓笔记】GestureDetector浅析
点击屏幕并拖动:
【安卓笔记】GestureDetector浅析
点击屏幕迅速滑动并松手:
【安卓笔记】GestureDetector浅析


【安卓笔记】GestureDetector浅析,布布扣,bubuko.com

【安卓笔记】GestureDetector浅析

上一篇:Android anr 产生的原因和如何避免


下一篇:第一期:从零到一上手玩转云服务器