Skip to content

BestGestureDetector

ChenChen edited this page Aug 28, 2023 · 6 revisions

效果

Screenrecorder-2023-08-28-16-21-50-618.mp4

用法

  1. 构造一个手势对象
//传入需要手势操作的View
val gesture = BestGestureDetector(view)
  1. 给手势对象设置监听
//必须设置的监听
gesture.setOnTouchListener(OnSimpleTouchListener())
  1. 接管onTouchEvent
override fun onTouchEvent(event: MotionEvent): Boolean {
    return gesture.onTouchEvent(event)
}

快速上手

一个简单的用法,快速实现缩放、旋转、平移手势

class GestureView : View {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    val gesture = BestGestureDetector(this).apply {
        setOnTouchListener(object : OnSimpleTouchListener() {
            override fun onTouchMove(detector: BestGestureDetector): Boolean {
                offsetLeftAndRight(detector.moveX.toInt())
                offsetTopAndBottom(detector.moveY.toInt())
                return super.onTouchMove(detector)
            }
        })
        setMoveListener(object : OnSimpleMoveListener() {
            override fun onMove(detector: BestGestureDetector): Boolean {
                offsetLeftAndRight(detector.moveX.toInt())
                offsetTopAndBottom(detector.moveY.toInt())
                return super.onMove(detector)
            }
        })
        setRotationListener(object : OnSimpleRotateListener() {
            override fun onRotate(detector: BestGestureDetector): Boolean {
                this@GestureView.rotation += detector.rotation
                return super.onRotate(detector)
            }
        })
        setScaleListener(object : OnSimpleScaleListener() {
            override fun onScale(detector: BestGestureDetector): Boolean {
                scaleX *= detector.scaleFactor
                scaleY *= detector.scaleFactor
                return super.onScale(detector)
            }
        })
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return gesture.onTouchEvent(event)
    }
}

API介绍

BestGestureDetector.setOnTouchListener()

设置触摸监听,必须设置,否则不会处理任何事件。OnTouchListener.onBeginTouch的返回值表示是否处理事件。如果觉得重写OnTouchListener比较麻烦,可以直接构造OnSimpleTouchListener,例如:

val gesture = BestGestureDetector(this).apply {
    //OnSimpleTouchListener.onBeginTouch默认返回true
    setOnTouchListener(OnSimpleTouchListener())
}

BestGestureDetector.setScaleListener()

设置双指缩放监听,可选设置。

BestGestureDetector.setRotationListener()

设置双指旋转监听,可选设置

BestGestureDetector.setMoveListener()

设置双指移动监听,可选设置

BestGestureDetector.scaleFactor

获取当前缩放比,用法:

view.sacleX *= detector.scaleFactor
view.sacleY *= detector.scaleFactor

BestGestureDetector.rotation

获取当前旋转角度,用法

view.rotation += detector.rotation

BestGestureDetector.moveX

获取当前x轴移动偏移量,用法

view.x += detector.moveX
//或
view.offsetLeftAndRight(detector.moveX.toInt())

BestGestureDetector.moveY

获取当前y轴移动偏移量,用法

view.y += detector.moveY
//或
view.offsetLeftAndRight(detector.moveY.toInt())

BestGestureDetector.consumeMove(x, y)

消费掉部分当前移动偏移量,多次调用累加消费,直到消费完本次所有的偏移量。这在传递手势的时候特别有用,传递给下一个需要处理的手势前,当前手势消费掉一定的偏移量,熟悉嵌套滑动的应该知道这种功能

BestGestureDetector.consumeRotation()

消费掉部分当前旋转角度,多次调用累加消费。

BestGestureDetector.consumeScale()

消费掉部分当前缩放比,多次调用累加消费。

BestGestureDetector.accumulateMoveX()

设置每当x轴移动了指定的值后才触发一次手势回调,如果一次移动过多,则会循环回调,直到移动偏移量小于指定的值才停止。应用场景未知,之前测试一个小功能写的,后来否决了这种方案。

BestGestureDetector.accumulateMoveY()

设置每当y轴移动了指定的值后才触发一次手势回调,如果一次移动过多,则会循环回调,直到移动偏移量小于指定的值才停止。

BestGestureDetector.accumulateRotation()

设置每当旋转了指定的值后才触发一次手势回调,如果一次旋转过多,则会循环回调,直到旋转值小于指定的值才停止。

BestGestureDetector.accumulateScale()

设置每当缩放了指定的值后才触发一次手势回调,如果一次缩放过多,则会循环回调,直到缩放值小于指定的值才停止。

BestGestureDetector.isEnableDoubleClick

设置是否开启双击,false 关闭双击,关闭双击后单击的响应会快一点,true 开启双击,开启双击后需要等待双击响应时间超时,单击响应就会慢一点

BestGestureDetector.isInSingleTapScrollingGiveUpClick

设置单击过程移动过就丢弃点击事件。原本单击时不允许移动超过MotionEvent.ACTION_DOWN时的多少多少的范围(不记得了),如果超过这次点击事件就被丢弃。

BestGestureDetector.isInDoubleTapScrollingGiveUpClick

设置双击过程移动过就丢弃点击事件。原本单击时不允许移动超过MotionEvent.ACTION_DOWN时的多少多少的范围(不记得了),如果超过这次点击事件就被丢弃。

Clone this wiki locally