android 扩大view的响应区域
1.Android提供TouchDelegate帮助实现扩大一个很小的view的点击区域
例如:https://developer.android.com/training/gestures/viewgroup.html#delegate
布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/parent_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <ImageButton android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@null"
- android:src="@drawable/icon" />
- </RelativeLayout>
代码
- // Get the parent view
- View parentView = findViewById(R.id.parent_layout);
- parentView.post(new Runnable() {
- // Post in the parent's message queue to make sure the parent
- // lays out its children before you call getHitRect()
- @Override
- public void run() {
- // The bounds for the delegate view (an ImageButton
- // in this example)
- Rect delegateArea = new Rect();
- ImageButton myButton = (ImageButton) findViewById(R.id.button);
- myButton.setEnabled(true);
- myButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Toast.makeText(MainActivity.this,
- "Touch occurred within ImageButton touch region.",
- Toast.LENGTH_SHORT).show();
- }
- });
- // The hit rectangle for the ImageButton
- myButton.getHitRect(delegateArea);
- // Extend the touch area of the ImageButton beyond its bounds
- // on the right and bottom.
- delegateArea.right += 100;
- delegateArea.bottom += 100;
- // Instantiate a TouchDelegate.
- // "delegateArea" is the bounds in local coordinates of
- // the containing view to be mapped to the delegate view.
- // "myButton" is the child view that should receive motion
- // events.
- TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
- myButton);
- // Sets the TouchDelegate on the parent view, such that touches
- // within the touch delegate bounds are routed to the child.
- if (View.class.isInstance(myButton.getParent())) {
- ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
- }
- }
- });
简单解释
1. 获取parentView并且在UIThread上post一个Runnable,确保viewGroup在绘制子view之前得到子view的getHintRect(),getHintRect()返回就是该view在viewGroup中的点击区域
2.Find这个View,并且通过getHintRect()得到点击区域,rect
3.扩大点击范围即扩大rect的left,right,top,bottom
4.初始化TouchDelegate,并且将扩大后的rect和view对象作为参数传入
5.在viewGroup中设置touchDelegate,viewGroup会自动扩大该view的响应区域
android 扩大view的响应区域的更多相关文章
- collectionviewcell 添加删除按钮 响应区域的问题
在collectionviewcell 的右上角添加了一个删除按钮,但是发现只有cell和删除按钮重合的区域才会响应点击事件 后来doctor 李说这是iOS 事件响应链的机制(http://www. ...
- 深入理解 Android 之 View 的绘制流程
概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...
- Android中View绘制优化之三---- 优化View
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译三: 优化视图 关于如何设计自定义View以及响应触摸时间等,请看Android developer : 地 ...
- Android自定义View(CustomCalendar-定制日历控件)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeas ...
- Android 开发 View的API 转载
转载地址:https://blog.csdn.net/lemonrabbit1987/article/details/47704679 View类代表用户界面组件的基本构建块.一个View占据屏幕上的 ...
- 【转】深入理解Android之View的绘制流程
概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...
- android 自定义view 前的基础知识
本篇文章是自己自学自定义view前的准备,具体参考资料来自 Android LayoutInflater原理分析,带你一步步深入了解View(一) Android视图绘制流程完全解析,带你一步步深入了 ...
- 【朝花夕拾】Android自定义View篇之(八)多点触控(上)MotionEvent简介
前言 在前面的文章中,介绍了不少触摸相关的知识,但都是基于单点触控的,即一次只用一根手指.但是在实际使用App中,常常是多根手指同时操作,这就需要用到多点触控相关的知识了.多点触控是在Android2 ...
- 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...
随机推荐
- Python爬虫二
常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...
- Root CA certificate:ApacheJMeterTemporaryRootCA.crt created in JMeter bin directory
今天学习jmeter录制,在点击start之后弹出: 且在jmeter安装目录里确实生成了ApacheJMeterTemporaryRootCA.crt文件 上网查询官方文档http://120.52 ...
- ssl 在nginx上的部署示例
server { listen 80; listen 443 ssl; server_name [DOMAIN]; ssl on; ssl_certificate /work/ss ...
- java静态代理模式
代理模式分为动态代理和静态代理. 静态代理简述: 1.为其他对象提供一种代理,以控制对这个对象的访问. 2.代理对象会起到中介的作用,可以增加些功能,也可以去掉某些功能. 静态代理: 代理和被代理对象 ...
- luogu3376 【模板】网络最大流 dinic
当前弧优化 #include <iostream> #include <cstring> #include <cstdio> #include <queue& ...
- python-高级编程-02
[yield 详解 协同程序 生成器表达式] 1> yield def res (): for i in range(10): x = yield i r = res() print r.nex ...
- js 获取data-属性值
].getAttribute('data-price'); 注意 document.getElementsByClassName('1pc_price')后面有[0],不然会报错.
- 利用Python分析羊车门问题
题目描述:有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么.参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择. ...
- PHP中define()和const定义常量的区别
在PHP中可以通过define()和const两种方式定义常量可是在开发中我们应该什么时候用define()定义常量,什么时候用const定义常量? 这两种方式定义常量的主要区别是什么? 从5.3版本 ...
- Flowerpot(单调队列)
描述 Farmer John has been having trouble making his plants grow, and needs your help to water them pro ...