日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)

發(fā)布時(shí)間:2024/3/12 Android 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這是手勢(shì)攔截類(lèi)的源碼。注釋,也加的隨地時(shí),方便閱讀理解。在源碼后面,會(huì)有使用案例。

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 手勢(shì)攔截類(lèi)。用于簡(jiǎn)化,攔截水平或垂直的手勢(shì)。**/ public class InterceptTouchListener implements View.OnTouchListener {// 上下文,建議用全局上下文// private Context mContext;// 按壓到屏幕時(shí)的坐標(biāo)private float[] mCoordinate;// 最小的滑動(dòng)距離,不同手機(jī)不同private int mMinimumSlop;// 兩個(gè)回調(diào)接口,當(dāng)觸發(fā)水平或垂直手勢(shì)時(shí),會(huì)被調(diào)用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:// 保存按壓到屏幕時(shí)的坐標(biāo)setCoordinate(event);break;case MotionEvent.ACTION_MOVE:if (isClickEvent(event)) {// 不處理點(diǎn)擊事件,因?yàn)椴糠謾C(jī)型的點(diǎn)擊事件會(huì)觸發(fā)ACTION_MOVE,所以這里要做過(guò)濾。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 如果用戶當(dāng)前正在進(jìn)行水平方向的手勢(shì)操作*/private boolean isHorizontalGestureEvent(MotionEvent event) {float slopX = getSlopX(event);// 滑動(dòng)距離,必須大于最小滑動(dòng)距離。return slopX > mMinimumSlop && slopX >= getSlopY(event);}/*** @return return true 如果用戶當(dāng)前正在進(jìn)行垂直方向的手勢(shì)操作*/private boolean isVerticalGestureEvent(MotionEvent event) {float slopY = getSlopY(event);// 滑動(dòng)距離,必須大于最小滑動(dòng)距離。return slopY > mMinimumSlop && getSlopX(event) < slopY;}/*** @return return true 如果并沒(méi)有移動(dòng),只是點(diǎn)擊而已。*/private boolean isClickEvent(MotionEvent event) {return getSlopX(event) == 0 && getSlopY(event) == 0;}/*** 保存坐標(biāo)*/private void setCoordinate(MotionEvent event) {mCoordinate[0] = event.getX();mCoordinate[1] = event.getY();}/*** @return 水平滑動(dòng)的距離*/private float getSlopX(MotionEvent event) {return Math.abs(event.getX() - mCoordinate[0]);}/*** @return 垂直滑動(dòng)的距離*/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 {/*** 當(dāng)用戶在進(jìn)行水平方向的操作時(shí),該方法會(huì)被調(diào)用** @param currentX 當(dāng)前的X坐標(biāo)* @param lastX 上一次的X坐標(biāo)* @return return true 如果要攔截事件。*/boolean onHorizontalGesture(float currentX, float lastX);}public interface OnVerticalGestureListener {/*** 當(dāng)用戶在進(jìn)行垂直方向的操作時(shí),該方法會(huì)被調(diào)用** @param currentY 當(dāng)前的Y坐標(biāo)* @param lastY 上一次的Y坐標(biāo)* @return return true 如果要攔截事件。*/boolean onVerticalGesture(float currentY, float lastY);}}

使用案例

// 設(shè)置觸摸事件,mDrawer可以是任意需要設(shè)置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語(yǔ)法版——————————————————————————————————————————————// Lambda語(yǔ)法版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;}));

從使用上,可以看出。這個(gè)類(lèi),簡(jiǎn)化了手勢(shì)操作的判斷。你可以把你的精力專(zhuān)注于:當(dāng)水平或垂直事件,被觸發(fā)時(shí)。你要做什么業(yè)務(wù),以及需不需要攔截手勢(shì)事件就行。下面是英文注釋版本:

這是,英文注釋版本:

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.

總結(jié)

以上是生活随笔為你收集整理的Android 手势拦截的实现(简化水平、垂直手势操作的拦截处理)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。