美文网首页
判断触摸事件是否在View内

判断触摸事件是否在View内

作者: CentForever | 来源:发表于2021-01-04 20:58 被阅读0次

判断触摸事件是否在View内

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            // 获取点击坐标
            val x = ev.rawX.toInt()
            val y = ev.rawY.toInt()
            canFinish = !(isTouchPointInView(rpItemPack, x, y) || isTouchPointInView(rpItemUnPack, x, y))
        }
        return super.dispatchTouchEvent(ev)
    }
    
    private fun isTouchPointInView(view: View, x: Int, y: Int): Boolean {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val left = location[0]
        val top = location[1]
        val right = left + view.measuredWidth
        val bottom = top + view.measuredHeight
        return y in top..bottom && x in left..right
    }

相关文章

网友评论

      本文标题:判断触摸事件是否在View内

      本文链接:https://www.haomeiwen.com/subject/vmagnktx.html