判断触摸事件是否在View内
判断触摸事件是否在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
网友评论