判斷觸摸事件是否在View內(nèi)
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
}