扩大View的点击范围
扩大View的点击范围本人知道的有两种方法,在不影响界面效果的前提下:
1、在View的外面添加一个透明容器
2、就是本文要说的,代码如下 :
public void addToParentArea(final View view) { DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
final float density = metric.density; final View parent = (View) view.getParent();
parent.post(new Runnable() {
public void run() {
// View的点击范围向四周扩大30个单位
final Rect r = new Rect();
view.getHitRect(r);
r.right += 30 * density;
r.left += 30 * density;
r.bottom += 30 * density;
r.top += 30 * density;
parent.setTouchDelegate(new TouchDelegate(r, view));
}
});
}
参考文章:http://developer.android.com/training/gestures/viewgroup.html
Android provides the TouchDelegate
class to make it possible for a parent to extend the touchable area of a child view beyond the child's bounds. This is useful when the child has to be small, but should have a larger touch region. You can also use this approach to shrink the child's touch region if need be.
In the following example, an ImageButton
is the "delegate view" (that is, the child whose touch area the parent will extend). Here is the layout file:
1: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2: android:id="@+id/parent_layout"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: tools:context=".MainActivity" >
6: <ImageButton android:id="@+id/button"
7: android:layout_width="wrap_content"
8: android:layout_height="wrap_content"
9: android:background="@null"
10: android:src="@drawable/icon" />
11: </RelativeLayout>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
The snippet below does the following:
- Gets the parent view and posts a
Runnable
on the UI thread. This ensures that the parent lays out its children before calling thegetHitRect()
method. ThegetHitRect()
method gets the child's hit rectangle (touchable area) in the parent's coordinates. - Finds the
ImageButton
child view and callsgetHitRect()
to get the bounds of the child's touchable area. - Extends the bounds of the
ImageButton
's hit rectangle. - Instantiates a
TouchDelegate
, passing in the expanded hit rectangle and theImageButton
child view as parameters. - Sets the
TouchDelegate
on the parent view, such that touches within the touch delegate bounds are routed to the child.
In its capacity as touch delegate for the ImageButton
child view, the parent view will receive all touch events. If the touch event occurred within the child's hit rectangle, the parent will pass the touch event to the child for handling.
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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);
}
}
});
}
}
扩大View的点击范围的更多相关文章
- view.performClick()触发点击事件
1.主要作用 自动触发控件的点击事件 2.界面的布局文件 activity_main.xml <RelativeLayout xmlns:android="http://schema ...
- Android 扩大 View 的点击区域
有时候,按照视觉图做出来效果后,发现点击区域过小,不好点击,用户体验肯定不好.扩大视图,就会导致整个视觉图变得不好看.那么有没有什么办法在不改变视图大小的前提下扩大点击区域呢? 答案是有! 能够解决这 ...
- Andorid-解决View重复点击的思路
Andorid-解决View重复点击的思路 转 https://www.jianshu.com/p/10d400a296fe 最近遇到一道面试题,题目是在App开发中,如何防止多次点击支付或者多次点击 ...
- 利用Kotlin扩展函数实现任意View的点击处理(点击效果和防止快速点击)
利用Kotlin扩展函数实现View的点击处理(点击效果和防止快速点击) kotlin经典写法: view?.setOnClickListener { //实现 } 项目实践证明,这种写法存在问题 例 ...
- 给View 添加手势,点击无反应 如何给View添加点击事件,手势方法
项目中有很多地方需要添加点击事件,重复代码很多,所以做了一个UIView的分类,专门做点击事件使用.项目地址:UIView-Tap 代码很简单,主要有一点就是注意分类不能直接添加属性,需要用到运行时相 ...
- iOS响应超出View范围点击事件
// 在view中重写以下方法,其中self.button就是那个希望被触发点击事件的按钮 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent ...
- Android 自定义View——自定义点击事件
每个人手机上都有通讯录,这是毫无疑问的,我们通讯录上有一个控件,在通讯录的最左边有一列从”#”到”Z”的字母,我们通过滑动或点击指定的字母来确定联系人的位置,进而找到联系人.我们这一节就通过开发这个控 ...
- ViewPager和Fragment中的View的点击事件冲突
ViewPager属于父布局,View属于子布局: 触摸事件是先到父View,再到子View,所以可以让ViewPager取消拦截事件: public class ComposeViewPager e ...
- android View的点击无效的原因
点击事件不生效,原来是因为我在里面的 ImageView中添加了 android:clickable="true". 解决办法:删掉ImageView中的android:click ...
随机推荐
- SQLSERVER 脚本转MYSQL 脚本的方法总结
1.MYSQL(版本为5.6)中SQL脚本必须以分号(;)结尾,这点比SQLSERVER要严谨:关键字与函数名称全部大写:数据库名称.表名称.字段名称全部小写. 2.所有关键字都要加上``,比如 St ...
- WCF学习心得------(七)消息协定
第七章 消息协定 7.1 消息协定概述 通常情况下,在定义消息的架构时只使用数据协定就足够,但是有时需要精确控制如何将类型映射到通过网络传输的SOAP消息.对于这种情况,通常解决方案是插入自定义的SO ...
- 每日学习心得:Linq解决DataTable按照某一列的值排序问题/DataTable 导出CSV文件/巧用text-overflow解决数据绑定列数据展示过长问题
2013-8-5 1 Linq解决DataTable按照某一列的值排序 在之前的总结中提到过对拼接而成的复合的DataTable按照某一列值的大小排序,那个主要的思想是在新建表结构时将要排序的那一列的 ...
- 如何创建自己的docker image并上传到DockerHub上
这里,记录一下比较常用的docker操作细节,对于初次使用者,可能有很大的帮助. docker作为云计算Paas层面的东西,风靡全世界了,主要是因为它小巧,好用,功能强大.今天主要介绍一下如何依据自己 ...
- Eclipse中添加web dynamic project
因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接: ...
- 【Spring-AOP-学习笔记-7】@Around增强处理简单示例
阅读目录 简单介绍 章节1:项目结构 章节2:定义切面类.连接点注解类 章节3:为待增强的方法--添加注解声明 章节4:AspectJ配置文件 章节5:测试类xxx 章节6:测试结果 Around 增 ...
- 【mysql】之MySQL导入sql脚本错误:2006 - MySQL server has gone away
到如一些小脚本很少报错,但最近导入一个10+M的SQL脚本,却重复报错: Error occured at:2014-03-24 11:42:24Line no.:85Error Code: 2006 ...
- VisualSVN Server以及TortoiseSVN客户端的配置和使用方法
http://www.cnblogs.com/beautifulFuture/archive/2014/07/01/3818211.html 近期学习代码管理工具,首先学习一下svn和Tortoise ...
- 【SQL Server】系统学习之二:索引优化
页大小8192个字节,行限制为8060字节(大型对象除外). 包含varchar nvarchar varbinary sql_variant(8012,object类型) clr 的行,如果行大小超 ...
- (C#) 调用执行批处理文件
Task: 在Windows的Service里面定时的调用执行一个批处理文件. private ApplicationOutput RunCommandOnPC(string executableP ...