Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)
生活随笔
收集整理的這篇文章主要介紹了
Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這是手勢攔截類的源碼。注釋,也加的隨地時,方便閱讀理解。在源碼后面,會有使用案例。
package com.laka.robotdog.widget;import android.content.Context; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration;/*** @Author Lyf* @CreateTime 2018/3/26* @Description 手勢攔截類。用于簡化,攔截水平或垂直的手勢。**/ public class InterceptTouchListener implements View.OnTouchListener {// 上下文,建議用全局上下文// private Context mContext;// 按壓到屏幕時的坐標private float[] mCoordinate;// 最小的滑動距離,不同手機不同private int mMinimumSlop;// 兩個回調接口,當觸發(fā)水平或垂直手勢時,會被調用private OnHorizontalGestureListener mOnHorizontalGestureListener;private OnVerticalGestureListener mOnVerticalGestureListener;public InterceptTouchListener(Context mContext) {mCoordinate = new float[2];mMinimumSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();}@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 保存按壓到屏幕時的坐標setCoordinate(event);break;case MotionEvent.ACTION_MOVE:if (isClickEvent(event)) {// 不處理點擊事件,因為部分機型的點擊事件會觸發(fā)ACTION_MOVE,所以這里要做過濾。break;} else {if (mOnHorizontalGestureListener != null&& isHorizontalGestureEvent(event)) {return mOnHorizontalGestureListener.onHorizontalGesture(event.getX(), mCoordinate[0]);}if (mOnVerticalGestureListener != null&& isVerticalGestureEvent(event)) {return mOnVerticalGestureListener.onVerticalGesture(event.getY(), mCoordinate[1]);}}break;case MotionEvent.ACTION_UP:break;}return false;}/*** @return return true 如果用戶當前正在進行水平方向的手勢操作*/private boolean isHorizontalGestureEvent(MotionEvent event) {float slopX = getSlopX(event);// 滑動距離,必須大于最小滑動距離。return slopX > mMinimumSlop && slopX >= getSlopY(event);}/*** @return return true 如果用戶當前正在進行垂直方向的手勢操作*/private boolean isVerticalGestureEvent(MotionEvent event) {float slopY = getSlopY(event);// 滑動距離,必須大于最小滑動距離。return slopY > mMinimumSlop && getSlopX(event) < slopY;}/*** @return return true 如果并沒有移動,只是點擊而已。*/private boolean isClickEvent(MotionEvent event) {return getSlopX(event) == 0 && getSlopY(event) == 0;}/*** 保存坐標*/private void setCoordinate(MotionEvent event) {mCoordinate[0] = event.getX();mCoordinate[1] = event.getY();}/*** @return 水平滑動的距離*/private float getSlopX(MotionEvent event) {return Math.abs(event.getX() - mCoordinate[0]);}/*** @return 垂直滑動的距離*/private float getSlopY(MotionEvent event) {return Math.abs(event.getY() - mCoordinate[1]);}public InterceptTouchListener setOnHorizontalGestureListener(OnHorizontalGestureListener mOnHorizontalGestureListener) {this.mOnHorizontalGestureListener = mOnHorizontalGestureListener;return this;}public InterceptTouchListener setOnVerticalGestureListener(OnVerticalGestureListener mOnVerticalGestureListener) {this.mOnVerticalGestureListener = mOnVerticalGestureListener;return this;}public interface OnHorizontalGestureListener {/*** 當用戶在進行水平方向的操作時,該方法會被調用** @param currentX 當前的X坐標* @param lastX 上一次的X坐標* @return return true 如果要攔截事件。*/boolean onHorizontalGesture(float currentX, float lastX);}public interface OnVerticalGestureListener {/*** 當用戶在進行垂直方向的操作時,該方法會被調用** @param currentY 當前的Y坐標* @param lastY 上一次的Y坐標* @return return true 如果要攔截事件。*/boolean onVerticalGesture(float currentY, float lastY);}}使用案例
// 設置觸摸事件,mDrawer可以是任意需要設置OnTouchListener的View.mDrawer.setOnTouchListener(new InterceptTouchListener(this).setOnHorizontalGestureListener(new InterceptTouchListener.OnHorizontalGestureListener() {@Overridepublic boolean onHorizontalGesture(float currentX, float lastX) {// 從左向右滑if (currentX - lastX > 0) {if (!mDrawer.isDrawerOpen(mDrawerContentView)) {mDrawer.openDrawer(Gravity.START);}} else {// 從右向左滑if (mDrawer.isDrawerOpen(mDrawerContentView)) {mDrawer.closeDrawer(Gravity.START);}}return true;}}));———————————————————————————————————Lambda語法版——————————————————————————————————————————————// Lambda語法版mDrawer.setOnTouchListener(new InterceptTouchListener(this).setOnHorizontalGestureListener((currX, lastX) -> {// 從左向右滑if (currX - lastX > 0) {if (!mDrawer.isDrawerOpen(mDrawerContentView)) {mDrawer.openDrawer(Gravity.START);}} else {// 從右向左滑if (mDrawer.isDrawerOpen(mDrawerContentView)) {mDrawer.closeDrawer(Gravity.START);}}return true;}));從使用上,可以看出。這個類,簡化了手勢操作的判斷。你可以把你的精力專注于:當水平或垂直事件,被觸發(fā)時。你要做什么業(yè)務,以及需不需要攔截手勢事件就行。下面是英文注釋版本:
這是,英文注釋版本:
package com.laka.robotdog.widget;import android.content.Context; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration;/*** @Author Lyf* @CreateTime 2018/3/26* @Description An InterceptTouchListener is a class for intercepting Gestures.* For Instance, moving finger from top to down or from left to right on Screen.**/ public class InterceptTouchListener implements View.OnTouchListener {// Context, It also can be an Application Context.// private Context mContext;// Coordinate of the point You pressed on Screen.private float[] mCoordinate;// Distance in pixels a touch can wander before we think the user is scrollingprivate int mMinimumSlop;// These interfaces definition for callback When the user vertically or horizontally moving his finger on the screen.private OnHorizontalGestureListener mOnHorizontalGestureListener;private OnVerticalGestureListener mOnVerticalGestureListener;public InterceptTouchListener(Context mContext) {mCoordinate = new float[2];mMinimumSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();}@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// Save the coordinate of the point you were click on the screen.setCoordinate(event);break;case MotionEvent.ACTION_MOVE:if (isClickEvent(event)) {// We don't deal with click Event in here.break;} else {if (mOnHorizontalGestureListener != null&& isHorizontalGestureEvent(event)) {return mOnHorizontalGestureListener.onHorizontalGesture(event.getX(), mCoordinate[0]);}if (mOnVerticalGestureListener != null&& isVerticalGestureEvent(event)) {return mOnVerticalGestureListener.onVerticalGesture(event.getY(), mCoordinate[1]);}}break;case MotionEvent.ACTION_UP:break;}return false;}/*** Horizontal Gesture Event is an event, from left to right or a reverse action.** @return return true if the user is moving his finger from left to right or from right to left.*/private boolean isHorizontalGestureEvent(MotionEvent event) {float slopX = getSlopX(event);return slopX > mMinimumSlop && slopX >= getSlopY(event);}/*** Horizontal Gesture Event is an event, from top to down or a reverse action.** @return return true if the user is moving his finger from top to down or from down to top.*/private boolean isVerticalGestureEvent(MotionEvent event) {float slopY = getSlopY(event);return slopY > mMinimumSlop && getSlopX(event) < slopY;}/*** Some phones will perform the ACTION_MOVE action When the user just clicked the screen.* This shouldn't be. Anyway, We gotta deal with it. So that I created this method.** @return return true if the action is just a pressed action.*/private boolean isClickEvent(MotionEvent event) {return getSlopX(event) == 0 && getSlopY(event) == 0;}private void setCoordinate(MotionEvent event) {mCoordinate[0] = event.getX();mCoordinate[1] = event.getY();}/*** Distance in horizontal orientation of the user moving on.*/private float getSlopX(MotionEvent event) {return Math.abs(event.getX() - mCoordinate[0]);}/*** Distance in vertical orientation of the user moving on.*/private float getSlopY(MotionEvent event) {return Math.abs(event.getY() - mCoordinate[1]);}public InterceptTouchListener setOnHorizontalGestureListener(OnHorizontalGestureListener mOnHorizontalGestureListener) {this.mOnHorizontalGestureListener = mOnHorizontalGestureListener;return this;}public InterceptTouchListener setOnVerticalGestureListener(OnVerticalGestureListener mOnVerticalGestureListener) {this.mOnVerticalGestureListener = mOnVerticalGestureListener;return this;}public interface OnHorizontalGestureListener {/*** Called When user is moving his finger in horizontally orientation.** @return return true if you want to intercept the touch Event.*/boolean onHorizontalGesture(float currentX, float lastX);}/*** Called When user is moving his finger in vertically orientation.** @return return true if you want to intercept the touch Event.*/public interface OnVerticalGestureListener {boolean onVerticalGesture(float currentY, float lastY);}}Thank you for reading my posted. If you have any questions, You can comment below. Thank you.
總結
以上是生活随笔為你收集整理的Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ★ZOJ 3380 Patchouli'
- 下一篇: (55)-- 简单爬取人人网个人首页信息