Android中基本手势判断

2021-12-24  本文已影响0人  wuuuuu_

View手势监听

直接上代码

// 首先设置监听
view.setOnTouchListener(this)

/**
*  重新方法onTouch()方法
*  基本的触摸事件有:
*  ACTION_DOWN、ACTION_MOVE、ACTION_UP、ACTION_CANCEL
*  多手指的事件,需要用到 :ACTION_MASK、ACTION_POINTER_DOWN、ACTION_POINTER_UP
*  ACTION_MASK是什么呢,可以理解它为event.action的位掩码
*  ACTION_POINTER_DOWN可以理解为多触点的down事件,ACTION_POINTER_UP同理
* 简单的单触摸事件监听下面几个action就可以了。
*/
override fun onTouch(v: View, event: MotionEvent): Boolean {
    when(event.action) {
        MotionEvent.ACTION_DOWN -> {}
        MotionEvent.ACTION_MOVE -> {}
        MotionEvent.ACTION_UP -> {}
        MotionEvent.ACTION_CANCEL -> {}
    }
    return true
}
/**
*  多触控就需要用到 ACTION_MASK
*  event.pointerCount 获取触摸点数
*
*/
override fun onTouch(v: View, event: MotionEvent): Boolean {
    val pointerCount = event.pointerCount
    when(event.action and MotionEvent.ACTION_MASK) {
        MotionEvent.ACTION_DOWN -> {
            downX = event.x
            downY = event.y
        }
        MotionEvent.ACTION_POINTER_DOWN -> {
            if (pointerCount == 1) {
                // 一根手指触摸
                downX = event.x
                downY = event.y
            }else if (pointerCount == 2) {
                // 两根手指触摸
                // 第一根手指触摸的坐标
                val downX1 = event.getX(0)
                val downY1 = event.getY(0)
                // 第二根手指触摸的坐标
                val downX2 = event.getX(1)
                val downY2 = event.getY(1)
                // 计算两个手指间距离,利用勾股定理 x^2 + y^2 = z^2
                val x = abs(downX1 - downX2)
                val y = abs(downY1 - downY2)
                // sqrt就是开正平方根
                downLenght = sqrt(x * x + y * y).toDouble()
            }
        }
        MotionEvent.ACTION_MOVE -> {
            if (pointerCount == 1) {
                // 一根手指触摸
            }else if (pointerCount == 2) {
                // 两根手指触摸
                // 第一根手指触摸的坐标
                val moveX1 = event.getX(0)
                val moveY1 = event.getY(0)
                // 第二根手指触摸的坐标
                val moveX2 = event.getX(1)
                val moveY2 = event.getY(1)
                // 计算两个手指间距离,利用勾股定理 x^2 + y^2 = z^2
                val x = abs(moveX1 - moveX2)
                val y = abs(moveY1 - moveY2)
                // sqrt就是开正平方根
                val moveLenght = sqrt(x * x + y * y).toDouble()
                // 通过判断down和move时两指距离的大小来判断是放大手势还是缩小手势
                if (moveLenght > downLenght) {
                    // 放大手势
                }else {
                    // 缩小手势
                }
            }
        }
        MotionEvent.ACTION_UP -> {}
        MotionEvent.ACTION_POINTER_UP -> {}
    }
    return true
}

android还有GestureDetector这一套手势监测的东西,就简单记录一下

val detector = GestureDetector(this, gestureListener )
detector.setOnDoubleTapListener(doubleTapListener)

private val gestureListener = object: OnGestureListener {
    boolean onDown(MotionEvent e){}
    void onShowPress(MotionEvent e){}
    boolean onSingleTapUp(MotionEvent e){}
    boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){}
    void onLongPress(MotionEvent e){}
    boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){}
}

private val doubleTapListener = object: OnDoubleTapListener {
    boolean onSingleTapConfirmed(MotionEvent e){}
    boolean onDoubleTap(MotionEvent e){}
    boolean onDoubleTapEvent(MotionEvent e){}
}

android系统也提供了一个SimpleOnGestureListener类,它实现了OnGestureListener, OnDoubleTapListener这两个接口,我们也可以把自己activity去继承SimpleOnGestureListener,然后去重写里面需要的方法。

class TouchView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : SurfaceView(context, attrs, defStyleAttr, 0), View.OnTouchListener,
    GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

    private var detector: GestureDetector

    init {
        setOnTouchListener(this)
        detector = GestureDetector(context, this)
        detector.setOnDoubleTapListener(this)
    }

    override fun onTouch(v: View?, event: MotionEvent): Boolean {
//        val pointerCount = event.pointerCount
//        when (event.action and MotionEvent.ACTION_MASK) {
//            MotionEvent.ACTION_DOWN -> {
//                Log.e("wu", "【ACTION_DOWN】pointerCount:$pointerCount")
//            }
//            MotionEvent.ACTION_MOVE -> {
//                Log.e("wu", "【ACTION_MOVE】pointerCount:$pointerCount")
//            }
//            MotionEvent.ACTION_UP -> {
//                Log.e("wu", "【ACTION_UP】pointerCount:$pointerCount")
//            }
//            MotionEvent.ACTION_POINTER_DOWN -> {
//                Log.e("wu", "【ACTION_POINTER_DOWN】pointerCount:$pointerCount")
//            }
//            MotionEvent.ACTION_POINTER_UP -> {
//                Log.e("wu", "【ACTION_POINTER_UP】pointerCount:$pointerCount")
//            }
//        }
//        return true
        return detector.onTouchEvent(event)
    }


    // region OnGestureListener
    override fun onDown(e: MotionEvent?): Boolean {
        Log.e("wu", "【onDown】pointerCount:${e?.pointerCount}")
        return true
    }

    override fun onShowPress(e: MotionEvent?) {
        Log.e("wu", "【onShowPress】作为视觉反馈给用户,提醒用户事件已被识别")
    }

    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        Log.e("wu", "【onSingleTapUp】轻点击")
        return true
    }

    override fun onScroll(
        e1: MotionEvent, e2: MotionEvent,
        distanceX: Float, distanceY: Float
    ): Boolean {
        Log.e("wu", "【onScroll】滑动, e1:${e1.pointerCount}, e2:${e2.pointerCount}")
        when (e2.pointerCount) {
            1 -> {

            }
            2 -> {

            }
            else -> {
            }
        }
        return true
    }

    override fun onLongPress(e: MotionEvent?) {
        Log.e("wu", "【onLongPress】长按事件")
    }

    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent?,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        Log.e("wu", "【onFling】用甩出去的方式结束滑动")
        return true
    }
    // endregion

    // region OnDoubleTapListener
    override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
        Log.e("wu", "【onSingleTapConfirmed】一次轻点击后触发,且不是双击事件")
        return true
    }

    override fun onDoubleTap(e: MotionEvent?): Boolean {
        Log.e("wu", "【onDoubleTap】双击")
        return true
    }

    override fun onDoubleTapEvent(e: MotionEvent?): Boolean {
        Log.e("wu", "【onDoubleTapEvent】双击事件发生时触发,包括down,move,up")
        return true
    }
    // endregion
}
上一篇下一篇

猜你喜欢

热点阅读