android GestureDetector使用
android为了增加用户体验,新增了GestureDetector类,那该类是做什么用的呢?
通俗的讲该类是 手势检测. 难道是手指有不同的动作系统会检测到,是的.确实如此.
哎,问题来了,那诡异了.那这个类和touch有什么关系呢?我们平常写代码的时候,如何是单击,我们都是通过挂载个listener来响应这个橱摸时间.那和这个类有关系吗? 恩,有关系的.
touch 是触摸,只要手指接触到屏幕都是touch. 为了增加用户体验.手指在屏幕滑动算不算橱摸呢?双击算不算橱摸呢?都是啊.
所以说. touch是个广泛的概念,基本用来响应单击事件,如果更细致的区分各中touch事件,那就需要该类了.
口说没用,举个例子最要紧,弄出APK是硬道理,来吧.
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class Res extends Activity implements View.OnTouchListener {
????Button btn = null;
????private GestureDetector mGestureDetector = null;
????/** Called when the activity is first created. */
????@Override
????public void onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????btn = (Button) findViewById(R.id.button);
????????btn.setOnTouchListener(this);
????????mGestureDetector = new GestureDetector(this, new LearnGestureListener());
????}
????public boolean onTouch(View view, MotionEvent event) {
????????return mGestureDetector.onTouchEvent(event);
????}
????class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
????????@Override
????????public boolean onSingleTapUp(MotionEvent ev) {
????????????Log.d("DEBUG","onSingleTapUp");
????????????return true;
????????}
????????@Override
????????public void onShowPress(MotionEvent ev) {
????????????Log.d("DEBUG","onShowPress");
????????}
????????@Override
????????public void onLongPress(MotionEvent ev) {
????????????Log.d("DEBUG","onLongPress");
????????}
????????@Override
????????public boolean onScroll(MotionEvent e1, MotionEvent e2,
????????????????float distanceX, float distanceY) {
????????????Log.d("DEBUG","onScroll");
????????????return true;
????????}
????????@Override
????????public boolean onDown(MotionEvent ev) {
????????????Log.d("DEBUG","onDownd");
????????????return true;
????????}
????????@Override
????????public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
????????????????float velocityY) {
????????????Log.d("DEBUG","onFling");
????????????return true;
????????}
????????public boolean onDoubleTap(MotionEvent event){
????????????Log.d("DEBUG","onDoubleTap");
????????????return true;
????????}
????}
}
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
????????return mGestureDetector.onTouchEvent(event);
文件的关键基本就这两句.第一句创建了对象,第二句把事件传给这个对象.传过去后系统回判断做什么动作.然后new LearnGestureListener()来调用相应的处理函数做处理.