TouchDelegate

/** * Helper class to handle situations where you want a view to have a larger touch area than its * actual view bounds. The view whose touch area is changed is called the delegate view. This * class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an * instance that specifies the bounds that should be mapped to the delegate and the delegate * view itself. * <p> * The ancestor should then forward all of its touch events received in its * {@link android.view.View#onTouchEvent(MotionEvent)} to {@link #onTouchEvent(MotionEvent)}. * </p> *

简单点说,就是一般我们只能Click这个View2的时候,View2才响应,但是我们想要Click这个View1的Bounds内,这个View2也要响应。

就是这么简单。。。

所以我们使用时候有:(注:bounds以view1为坐标)

  1. TouchDelegate td = new TouchDelegate(bounds,  view2);  
  2. view1.setTouchDelegate(td); 

eg:点击iv1,iv2产生效果

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_marginLeft="100dp"
        android:background="@drawable/list_item_bg" >
    </ImageView>

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_below="@id/iv1"
        android:background="@drawable/night_list_item_bg" >
    </ImageView>

</RelativeLayout>


java:

           Rect rect = new Rect(0, 0, iv1.getWidth(), iv1
                                .getHeight());
                        System.out.println(rect.toString());
                        iv1.setTouchDelegate(new TouchDelegate(rect, iv2));



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值