Android 判断手指滑出View边界,本身和父布局两个方法判断

从本身的角度判断:

当前触摸点是否滑出本身View边界

        val vibrator = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator        
        //手势监听
        mouse_mat?.setMouseTouchListener { event ->
            if (event.y < 0) { //手指划出上边界触发震动
                vibrator.vibrate(100)
            }
            //x<0 可判断滑出左边界
            //y>当前view的高度,表示滑出底部
        }

从父布局的角度判断:

传入viewGroup的event.x和event.y.    得知当前触摸点是否位于指定的子view区域:

private var mChangeImageBackgroundRect: Rect? = null
private fun isInChangeImageZone(view: View, x: Int, y: Int): Boolean {
    if (null == mChangeImageBackgroundRect) {
        mChangeImageBackgroundRect = Rect()
    }
    view.getDrawingRect(mChangeImageBackgroundRect)
    val location = IntArray(2)
    view.getLocationOnScreen(location)
    mChangeImageBackgroundRect!!.left = location[0]
    mChangeImageBackgroundRect!!.top = location[1]
    mChangeImageBackgroundRect!!.right = mChangeImageBackgroundRect!!.right + location[0]
    mChangeImageBackgroundRect!!.bottom = mChangeImageBackgroundRect!!.bottom + location[1]
    return mChangeImageBackgroundRect!!.contains(x, y)
}

Guess you like

Origin blog.csdn.net/qq_39731011/article/details/120078961