如何使得点击区域大于实际显示大小?
场景:这个图片或者文字太小了,用户很难点击,产品说,哇这样不行,一定要让用户好点击!
这个问题已经是早有解决方案,我只是把到收集的方法进行整理下。
[小招]对于图片,可以使用ImageButton来解决。
例:<ImageButton android:id="@+id/iBtnTest" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_above="@+id/btnAdd" android:background="@null" android:src="@mipmap/ic_launcher"/>
图片会按照自身尺寸,已经当前手机dpi进行放缩,点击区域为layout_width, layout_height 。
[大招] 通吃大部分场景,用TouchDelegate进行点击区域委托。
例:
// 1、设置 ImageButton 可点击的范围 Rect delegateArea = new Rect(); mImageButton.getHitRect(delegateArea); // Extend the touch area of the button beyond its bound on the right and bottom // 2、扩大 ImageButton 的点击范围 delegateArea.right += 100; delegateArea.bottom +=500; // Instantiate a TouchDelegate // 3、实例化 TouchDelegate TouchDelegate touchDelegate = new TouchDelegate(delegateArea, mImageButton); // Sets the TouchDelegate on the parent view, such that touches within the touch delegate // are routed to the child. ///4、将 touchDelegate 设置到 ImageButton 的父视图上。 if (View.class.isInstance(mImageButton.getParent())){ ((View)mImageButton.getParent()).setTouchDelegate(touchDelegate); }
转至yxhuang2008的专栏(点击我进入)的代码,里面还有详细的原理解析,有兴趣的同学可以看看。
如果还有新的方法,会继续更新到此篇文章。未完待续…