增大view的有效区域
来源:互联网 发布:mac 不生成 ds store 编辑:程序博客网 时间:2024/05/01 06:44
Android提供了一个TouchDelegate
类去让父类扩展它的子view的触摸区域。当子view很小但需要大的触摸区域的时候,这个类大有用处。如果你想要的话,你也能用这个类去缩小子view的触摸区域。
在下面的例子中,有一个作为例子的ImageButton
(也就是说父类会扩展这个子view的触摸区域)
、
在下面的例子中,有一个作为例子的ImageButton
(也就是说父类会扩展这个子view的触摸区域)
<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>
下面的代码段会完成下面的事情:
- 获取父view并post一个
Runnable
到UI线程。这会确保父类在调用getHitRect()
)方法前先勾画出他的子类。getHitRect()
)方法的作用是在父类的坐标系中获取子view的hit rectangle(触摸区域)。 - 找到
ImageButton
子view并调用getHitRect()
)去获取子类触摸区域范围。 - 扩展
ImageButton
的hit rectangle范围。 - 初始化
TouchDelegate
对象,参数是扩展后的hit rectangle和ImageButton
子view。 - 在父view中设置
TouchDelegate
,这样在这个触摸范围内的touch event都会传给ImageButton
在ImageButton
子view的触摸范围容量内,父view会接收所有的touch events,如果touch event发生在子类的hit rectangle内,父类会将touch event传给子类做处理。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取父view View parentView = findViewById(R.id.parent_layout); parentView.post(new Runnable() { // post到父类的消息队列中,确保在调用getHitRect()前勾画出子类 @Override public void run() { // 实例view的区域范围(ImageButton) 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(); } }); // ImageButton的hit rectangle myButton.getHitRect(delegateArea); // 在ImageButton边框的右边和底边扩展触摸区域 delegateArea.right += 100; delegateArea.bottom += 100; // 初始化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); } } }); }}
0 0
- 增大view的有效区域
- TouchDelegate 来增大按钮的点击区域
- Android 增大按钮的点击区域
- UIButton增大响应区域
- android开发如何增大控件的可点击区域
- 代码增大按钮响应区域
- 如何自定义button的点击有效区域
- 如何自定义button的点击有效区域
- 健按摩是采用按摩胸部、乳房的办法来增大乳房,方便有效。具体方法
- [Swift] UIButton 增大UIButton点击区域
- 有效区域和无效区域
- 获取View的有效屏幕大小
- 获取View的有效屏幕大小
- NYOJ92~图像有效区域
- 使用TouchDelegate扩大View的点击区域
- 关于View扩展点击区域的误区
- 使用TouchDelegate扩大View的点击区域
- 动态增大的栈
- 一致性hash算法
- 贫血模型和充血模型
- oracle数据库导入导出命令!
- python实现局域网ip地址扫描
- Object-C 协议@protocol
- 增大view的有效区域
- GNU 汇编中的#define 等宏定义解释
- Android可穿戴联盟来袭,苹果还在“憋大招”!
- linux下svn命令使用大全
- APK 安装过程 及 原理 详解
- Eclipse 无法打断点问题-遇到带斜杠的断点图标
- Oracle 判断 并 手动收集 统计信息 脚本
- UISlider常用操作
- Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法