当前位置: 首页 > news >正文

Android 扩大View的点击区域

文章目录

  • Android 扩大View的点击区域
    • 使用padding属性
    • 使用TouchDelegate
    • 使用getLocationOnScreen监听

Android 扩大View的点击区域

使用padding属性

通过设置 padding 属性扩大点击区域。

使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="20dp"android:text="hello" /><TextViewandroid:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="40dp"android:text="world" /></LinearLayout>

使用TouchDelegate

TouchDelegate 类是 Android 中的一个辅助类,可以用于扩展 View 的触摸区域,如小按钮。

  • 目标View必须有父 View。
  • 给多个 View 扩大点击区域时,不能是同一个父 View,从 View 类的源码中可知,设置 setTouchDelegate 时,会覆盖之前的。

扩展类:

fun View.expandTouchView(expandSize: Int = 15.dp) {val parentView = parent as? ViewparentView?.post {val rect = Rect()getHitRect(rect)Log.e("TAG", "rect = $rect")rect.left -= expandSizerect.top -= expandSizerect.right += expandSizerect.bottom += expandSizeLog.e("TAG", "expandRect = $rect")parentView.touchDelegate = TouchDelegate(rect, this)}
}

使用:

val text1 = findViewById<TextView>(R.id.text1)
val text2 = findViewById<TextView>(R.id.text2)text1.run {expandTouchView()setOnClickListener {ToastUtils.show((it as TextView).text.toString())}
}text2.run {expandTouchView(40.dp)setOnClickListener {ToastUtils.show((it as TextView).text.toString())}
}

使用getLocationOnScreen监听

  • 使用 getLocationOnScreen() 可以获取目标 View 在屏幕中的坐标。
  • RectF 是一个用于表示浮点坐标的矩形区域的类,可以用于设置扩大区域。
  • 通过监听 onTouchEvent() 触摸方法获取触摸的坐标,然后判断是否在扩大区域内。

自定义View:

class ParentTouchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {private lateinit var text1: TextViewoverride fun onFinishInflate() {super.onFinishInflate()if (childCount > 0) {text1 = findViewById(R.id.text1)text1.setOnClickListener {ToastUtils.show(text1.text.toString())}}}override fun onTouchEvent(event: MotionEvent?): Boolean {event?.let { e ->if (e.action == MotionEvent.ACTION_DOWN) {if (isExpandView(text1, e.rawX, e.rawY, 100.dp)) {text1.performClick()}}}return super.onTouchEvent(event)}/*** 是否在View的扩大区域** @param targetView 目标View* @param touchX 点击的位置* @param touchY 点击的位置* @param expandSize 扩大区域的大小* @return*/private fun isExpandView(targetView: View,touchX: Float,touchY: Float,expandSize: Int = 15.dp): Boolean {// 获取目标View的Rectval rect = RectF()val location = IntArray(2)// 获取目标View的坐标targetView.getLocationOnScreen(location)val childX = location[0].toFloat()val childY = location[1].toFloat()rect.set(childX,childY,childX + targetView.width,childY + targetView.height)// 设置扩大区域后的Rectrect.apply {left -= expandSizetop -= expandSizeright += expandSizebottom += expandSize}// 判断是否在扩大区域内return rect.contains(touchX, touchY)}
}

使用:

<?xml version="1.0" encoding="utf-8"?>
<com.example.xxx.view.ParentTouchView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"tools:context=".clickarea.LocationOnScreenActivity"><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hello" /></com.example.xxx.view.ParentTouchView>
http://www.lryc.cn/news/458469.html

相关文章:

  • [Qt学习笔记] 解决QTextEdit数据过多UI卡死问题
  • OgreNext高级材质中增加线宽,点大小,虚线模式绘制支持
  • STM32中的DMA数据转运——下篇
  • 51单片机的智能小区安防系统【proteus仿真+程序+报告+原理图+演示视频】
  • 数仓建模流程
  • Neo4j CQL语句 使用教程
  • STM32-HAL库 驱动DS18B20温度传感器 -- 2024.10.8
  • HTML 符号
  • 编译后的MySQL安装
  • Ubuntu安装Apache教程
  • Nginx跳转模块之location与rewrite
  • oracle col命令
  • ESP32接入扣子(Coze) API使用自定义智能体
  • 【JVM】如何判断对象是否可以被回收
  • CloseableHttpResponse 类(代表一个可关闭的 HTTP 响应)
  • C语言编程规范及命名规则
  • Pika 1.5 - Pika Labs最新推出的AI视频生成工具
  • Elon Musk 正式揭晓了特斯拉的Robotaxi和Robovan,这两款自动驾驶新车型成为发布会上的焦点
  • 博客摘录「 springboot+netty+mqtt服务端实现」2024年10月11日
  • Docker小白入门笔记
  • Unity 如何在 iOS 新增键盘 KeyCode 响应事件
  • pytorh学习笔记——波士顿房价预测
  • 让AI像人一样思考和使用工具,reAct机制详解
  • Linux系列-常见的指令(二)
  • Leecode刷题之路第17天之电话号码的字母组合
  • 2023牛客暑期多校训练营3(题解)
  • Magnum IO
  • Flink job的提交流程
  • git操作pull的时候出现冲突怎么解决
  • Sentinel 1.80(CVE-2021-44139)