Android 多手势开发 (1-5指)

需求:多指手势功能性需求

1:双指操作:双指同时在屏幕左半侧向上或向下滑动为音量增加或减少 双指同时在屏幕右半侧向上或向下滑动为背光亮度增加或减少 双指同时在整个屏幕向左或向右滑动为切换上或下一曲 双指同时从左/右屏幕边缘往中间滑动打开左/右快捷应用栏

2:三指操作:三指从上往下滑进入导航功能 三指从下往上滑进入屏保 三指从左往右滑打开指定应用软件 三指从右往左滑返回主页

3:四指操作:长按30s自动上传log 四指下滑关闭屏幕

4:五指操作:五指长按20s进入触摸校正 

开发:

package com.emsm.app.gesture;

import android.content.Context;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;

import androidx.annotation.NonNull;

import com.core.ex.LogHelps;
import com.core.ex.util.HandlerHelps;
        
/**
 * @Author chentao 0000668668
 * @Time 2023/6/15
 * @Description 多手势管理类 【支持1-5指长按,上下左右滑动区分】
 *
 */
public class MultiGestureManager {
    private IListener mListener;
    private FingerSplit mFingerSplit;
    private VelocityTracker mVelocityTracker;
    private int mMinimumFlingVelocity;
    private int mMaximumFlingVelocity;
    private int minVelocity = 50;

    public MultiGestureManager(Context context, IListener listener) {
        mListener = listener;
        mFingerSplit = new FingerSplit();
        if (context == null) {
            mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
            mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity();
        } else {
            final ViewConfiguration configuration = ViewConfiguration.get(context);
            mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
            mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity();
        }
    }

    private long[] mGingerLongClickTime;

    /**
     * 设置手指长按的时间
     *
     * @param delayMillis
     * @return
     */
    public MultiGestureManager setGingerLongClickTime(long... delayMillis) {
        if (delayMillis == null || delayMillis.length != 5) {
            new StringIndexOutOfBoundsException("delayMillis != null && delayMillis.length == 5");
        }
        mGingerLongClickTime = delayMillis;
        return this;
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mListener.onDown(event);
        }

        final boolean multiFingerProcessed = mFingerSplit.onTouchEvent(event);

        if (event.getAction() == MotionEvent.ACTION_UP) {
            mListener.onUp(event);
        }
        return multiFingerProcessed;
    }

    public static final int SINGLE_GINGER = 1, SINGLE_GINGER_LONG_CLICK = SINGLE_GINGER;
    public static final int DOUBLE_GINGER = 2, DOUBLE_GINGER_LONG_CLICK = DOUBLE_GINGER;
    public static final int THREE_GINGER = 3, THREE_GINGER_LONG_CLICK = THREE_GINGER;
    public static final int FOUR_GINGER = 4, FOUR_GINGER_LONG_CLICK = FOUR_GINGER;
    public static final int FIVE_GINGER = 5, FIVE_GINGER_LONG_CLICK = FIVE_GINGER;

    private Handler mHandler = HandlerHelps.createHandler(new Handler.Callback() {

        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SINGLE_GINGER_LONG_CLICK:
                    mListener.onLongPress(GestureEvent.SINGLE_GINGER_LONG_CLICK);
                    break;
                case DOUBLE_GINGER_LONG_CLICK:
                    mListener.onLongPress(GestureEvent.DOUBLE_GINGER_LONG_CLICK);
                    break;
                case THREE_GINGER_LONG_CLICK:
                    mListener.onLongPress(GestureEvent.THREE_GINGER_LONG_CLICK);
                    break;
                case FOUR_GINGER_LONG_CLICK:
                    mListener.onLongPress(GestureEvent.FOUR_GINGER_LONG_CLICK);
                    break;
                case FIVE_GINGER_LONG_CLICK:
                    mListener.onLongPress(GestureEvent.FIVE_GINGER_LONG_CLICK);
                    break;
            }
            return false;
        }
    });

    private class FingerSplit {
        private MotionEvent mStartMultiEvent;
        private MotionEvent mLastMultiEvent;
        private float mLastFocusX;
        private float mLastFocusY;
        private float mDownFocusX;
        private float mDownFocusY;

        private int mRecordMaxPointerCount;

        public boolean onTouchEvent(MotionEvent ev) {
            if (mVelocityTracker == null) {
                mVelocityTracker = VelocityTracker.obtain();
            }
            mVelocityTracker.addMovement(ev);

            boolean handled = false;
            float sumX = 0, sumY = 0;
            float focusX = 0, focusY = 0;

            mRecordMaxPointerCount = Math.max(mRecordMaxPointerCount, ev.getPointerCount());
            if (mRecordMaxPointerCount == ev.getPointerCount()) {
                for (int i = 0; i < mRecordMaxPointerCount; i++) {
                    sumX += ev.getX(i);
                    sumY += ev.getY(i);
                }
                focusX = sumX / mRecordMaxPointerCount;
                focusY = sumY / mRecordMaxPointerCount;
            }

            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    LogHelps.i("ACTION_DOWN_recordMaxPointerCount:" + mRecordM
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款用于开发Android应用程序的集成开发环境(IDE)。它提供了丰富的功能和工具,其中包括手势识别功能。 Android Studio中的手势识别是通过触摸屏幕上的手势来实现特定操作或交互。Android系统提供了GestureDetector类来处理手势识别,它可以用于检测和处理单击、长按、滑动、双击等手势。 要在Android Studio中使用手势识别功能,你可以按照以下步骤进行操作: 1. 在布局文件中添加一个View组件,用于接收用户的手势输入。 2. 在Java代码中实例化GestureDetector对象,并重写其回调方法,以响应不同的手势事件。 3. 将GestureDetector对象与View组件进行关联,通过设置触摸监听器来监听用户的手势输入。 4. 在回调方法中编写相应的逻辑,根据不同的手势事件执行相应的操作。 以下是一个简单的示例代码,演示如何在Android Studio中实现手势识别: ```java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener { private GestureDetector gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View view = findViewById(R.id.my_view); // 替换为你的View组件的ID gestureDetector = new GestureDetector(this, this); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } @Override public boolean onDown(MotionEvent e) { // 手按下时触发 return true; } @Override public void onShowPress(MotionEvent e) { // 手按下后未移动或松开时触发 } @Override public boolean onSingleTapUp(MotionEvent e) { // 单击时触发 return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // 滑动时触发 return true; } @Override public void onLongPress(MotionEvent e) { // 长按时触发 } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 快速滑动时触发 return true; } } ``` 这是一个简单的示例,你可以根据自己的需求进行扩展和修改。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值