Android: How to handle right to left swipe gestures

前端 未结 22 1214
日久生厌
日久生厌 2020-11-21 06:18

I want my app to recognize when a user swipes from right to left on the phone screen.

How to do this?

22条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:16

    A little modification of @Mirek Rusin answer and now you can detect multitouch swipes. This code is on Kotlin:

    class OnSwipeTouchListener(ctx: Context, val onGesture: (gestureCode: Int) -> Unit) : OnTouchListener {
    
    private val SWIPE_THRESHOLD = 200
    private val SWIPE_VELOCITY_THRESHOLD = 200
    
    private val gestureDetector: GestureDetector
    
    var fingersCount = 0
    
    fun resetFingers() {
        fingersCount = 0
    }
    
    init {
        gestureDetector = GestureDetector(ctx, GestureListener())
    }
    
    override fun onTouch(v: View, event: MotionEvent): Boolean {
        if (event.pointerCount > fingersCount) {
            fingersCount = event.pointerCount
        }
        return gestureDetector.onTouchEvent(event)
    }
    
    private inner class GestureListener : SimpleOnGestureListener() {
    
        override fun onDown(e: MotionEvent): Boolean {
            return true
        }
    
        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            var result = false
            try {
                val diffY = e2.y - e1.y
                val diffX = e2.x - e1.x
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            val gesture = when (fingersCount) {
                                1 -> Gesture.SWIPE_RIGHT
                                2 -> Gesture.TWO_FINGER_SWIPE_RIGHT
                                3 -> Gesture.THREE_FINGER_SWIPE_RIGHT
                                else -> -1
                            }
                            if (gesture > 0) {
                                onGesture.invoke(gesture)
                            }
                        } else {
                            val gesture = when (fingersCount) {
                                1 -> Gesture.SWIPE_LEFT
                                2 -> Gesture.TWO_FINGER_SWIPE_LEFT
                                3 -> Gesture.THREE_FINGER_SWIPE_LEFT
                                else -> -1
                            }
                            if (gesture > 0) {
                                onGesture.invoke(gesture)
                            }
                        }
                        resetFingers()
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        val gesture = when (fingersCount) {
                            1 ->  Gesture.SWIPE_DOWN
                            2 -> Gesture.TWO_FINGER_SWIPE_DOWN
                            3 -> Gesture.THREE_FINGER_SWIPE_DOWN
                            else -> -1
                        }
                        if (gesture > 0) {
                            onGesture.invoke(gesture)
                        }
                    } else {
                        val gesture = when (fingersCount) {
                            1 ->  Gesture.SWIPE_UP
                            2 -> Gesture.TWO_FINGER_SWIPE_UP
                            3 -> Gesture.THREE_FINGER_SWIPE_UP
                            else -> -1
                        }
                        if (gesture > 0) {
                            onGesture.invoke(gesture)
                        }
                    }
                    resetFingers()
                }
                result = true
    
            } catch (exception: Exception) {
                exception.printStackTrace()
            }
    
            return result
        }
    }}
    

    Where Gesture.SWIPE_RIGHT and others are unique integer indentificator of gesture that I`m using to detect kind of gesture later in my activity:

    rootView?.setOnTouchListener(OnSwipeTouchListener(this, {
        gesture -> log(Gesture.parseName(this, gesture))
    }))
    

    So you see gesture here is an integer variable that holds value I have passed before.

提交回复
热议问题