判断是否点击的事件是否在某一个view区间内

该代码片段是Android utility类,包含两个静态方法。`isTouchPointInView`用于判断给定的屏幕坐标是否在指定View内,而`mGetLocation`则返回View在屏幕上的边界矩形。这些函数在处理触摸事件或者视图布局时非常有用。
摘要由CSDN通过智能技术生成

```java
在这里插入代码片

package com.coloros.ocrscanner.translator.screen.utils

import android.graphics.Rect
import android.view.View
import com.coloros.ocrscanner.utils.LogUtils

object PointInView {

    private const val TAG = "PointInView"

    @JvmStatic
    fun isTouchPointInView(view: View?, x: Float, y: Float): Boolean {
        if (view == null) {
            return false
        }
        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
        if (y.toInt() in top..bottom && x >= left
            && x <= right
        ) {
            return true
        }
        return false
    }

    fun mGetLocation(view: View?): Rect {
        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
        var mViewRect: Rect = Rect()
        mViewRect.left = left
        mViewRect.right = right
        mViewRect.top = top
        mViewRect.bottom = bottom
        LogUtils.d(TAG, "left:$left top:$top right:$right bottom:$bottom")
        return mViewRect
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值