/** * 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为坐标)
- TouchDelegate td = new TouchDelegate(bounds, view2);
- 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));