android SimpleOnGestureListener详解

来源:互联网 发布:mac版炉石传说打不开 编辑:程序博客网 时间:2024/05/17 21:53

我们在做一些View滑动或者要求双击 快速滑动处理业务的时候还是比较复杂的,而android给我们封装提供了常用的类,比如双击监听android给我们提供了OnDoubleTapListener,比如滑动或者按下再抬起的操作,这个android也帮我封装了好了,实现这个接口SimpleOnGestureListener就行,这个SimpleOnGestureListener类是实现了OnGestureListener,由于这个接口方法过多,所以使用了类SimpleOnGestureListener去实现OnGestureListener接口,你想覆盖什么方法就重写就行,现在新建一个android项目来玩玩这里面几个方法,方便以后我们如果遇到自定义view可能会需要这个方面的知识,

/** * AUTHOR:Zhou Guizhi * * DESCRIPTION:create the File, and add the content. * * Copyright © ZhiMore. All Rights Reserved * */package com.zhimore.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;/** * Created in Mar 1, 2016 9:04:16 AM * @author Zhou Guizhi;  */public class CustomView extends View implements OnTouchListener {public static final String TAG = CustomView.class.getSimpleName();private GestureDetector gestureDetector;private MyOnGestureListener listener;private Paint mPaint;/** * @param context * @param attrs * @param defStyleAttr */public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/** * @param context * @param attrs */public CustomView(Context context, AttributeSet attrs) {super(context, attrs);mPaint = new Paint();setOnTouchListener(this);listener = new MyOnGestureListener();gestureDetector = new GestureDetector(context, listener);}/** * @param context */public CustomView(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);mPaint.setColor(Color.WHITE);mPaint.setTextSize(40);canvas.drawText("点我", 100, 100, mPaint);}@Overridepublic boolean onTouch(View v, MotionEvent event) {gestureDetector.onTouchEvent(event);return true;} class MyOnGestureListener extends SimpleOnGestureListener {@Overridepublic boolean onDown(MotionEvent e) {Log.e(TAG,"onDown------------>");return true;}@Overridepublic void onShowPress(MotionEvent e) {Log.e(TAG,"onShowPress------------>");}@Overridepublic boolean onSingleTapUp(MotionEvent e) {Log.e(TAG,"onSingleTapUp------------>");return false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {Log.e(TAG,"onScroll------------>");return false;}@Overridepublic void onLongPress(MotionEvent e) {Log.e(TAG,"onLongPress------------>");}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {Log.e(TAG,"onFling------------>");return false;} }}

SimpleOnGestureListener的使用步骤

1:初始化GestureDetector这个对象,这个对象里面封装了touch操作,而SimpleOnGestureListener这个类时真正的封装了touch里面的操作

2:把自定义view的touch事件交给GestureDetector去实现,它里面做了touch很多复杂的业务操作,有兴趣的话可以点击源码进去看看,然后touch返回值直接为true,表示touch事件自己处理,

@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}

然后我们打log玩下SimpleOnGestureListener类中几个常用的方法:

1):当我们手指在view上按下马上抬起的时候 执行的方法是onDown()----->onSingleTapUp()

2):当我们手指按下并停留一下在view上再抬起的时候执行的方法是onDown()---onShowPress()-----onSingleTapUp()

3):当我们手指在view上滑动一下而不是瞬间抬起的时候,执行的方法是onDown()---onShowPress()-----onScroll()

4):当我们手指在view上快速滑动然后抬起,执行的方法是onDown()---onShowPress()-----onScroll()--onFling()

5):当我们手指按住view并且不滑动停留在view上一段时间,执行的方法是onDown()---onShowPress()----onLongPress()


通过上面的打log日记分析我们对上面几个方法做一下总结:

onDown():刚刚手指接触到触摸屏的那一刹那,就是触的那一下。

onSingleTapUp():手指likaiview那一瞬间执行

onShowPress():手指按在触摸屏上,它的时间范围在按下起后,在长按之前

onScroll():手指在屏幕上滑动

onLongPress():手指在屏幕上停留一段时间 后抬起

onFling():手指在触摸屏上迅速移动,并松开的动作(滑动的比onScroll快)


我们重点是研究onScroll和onFling()这二个方法,也是在自定义view中使用最多的二个方法,主要是看这个二个方法中的形参参数表示的是什么意思,

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

Log.e(TAG, "distanceX--->"+distanceX);
Log.e(TAG, "distanceY--->"+distanceY);
 return false;
  }

当我们手指在y轴上向下滑动log日记为:


当我手指向上滑动在y轴上:


我们从log日记中可以判断当我们手指向下滑动的是表示负数,向上滑动是正数,这个数是表示距离,所以distanceY表示的是move二次滑动的距离,

现在对onFling()方法进行一样的分析:

手指向下滑动:

 velocityX--->209.28287
      velocityY--->-427.69553

向上滑动

velocityX--->-6.6459694
      velocityY--->446.57785

 velocity这个词表示的是速度的意思,所以这二个参数我们知道表示的是在x,y轴上滑动的速度,

现在写一个例子,就是一个view随便在屏幕上拖动,其实就是在onScroll()方法中处理一些简单的逻辑

@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {int top = getTop();int r = getRight();int b = getBottom();int l = getLeft();Log.e(TAG,"top--->"+top);Log.e(TAG,"r--->"+r);Log.e(TAG,"b--->"+b);Log.e(TAG,"l--->"+l);int newL = l-(int)distanceX;int newT = top-(int)distanceY;int newR = r-(int)distanceX;int newB = b-(int)distanceY;layout(newL, newT,newR,newB);return true;}

这就实现了view在屏幕随意拖动




0 0
原创粉丝点击