增大view的有效区域

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>

下面的代码段会完成下面的事情:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值