本文主要是介绍【安卓笔记】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;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mGestureDector = new GestureDetector(this,this);}@Overridepublic boolean onTouchEvent(MotionEvent event){//监听手势事件return mGestureDector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e){Log.i(TAG,"onDown run..");return false;}@Overridepublic void onShowPress(MotionEvent e){Log.i(TAG,"onShowPress run..");}@Overridepublic 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;}@Overridepublic void onLongPress(MotionEvent e){Log.i(TAG,"onLongPress run.."); }//用户按下触摸屏、快速移动后松开@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){Log.i(TAG,"onFling run..");return false;}}
测试:
点击屏幕:
点击屏幕不松手:
点击屏幕并拖动:
点击屏幕迅速滑动并松手:
这篇关于【安卓笔记】GestureDetector浅析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!