Android Touch(3)View的touchDelegate

作用:

基类View有个函数 public void setTouchDelegate(TouchDelegate delegate),给view内部的另一个view设置一个touch代理。

 

图中view是外部view,它包含tv1,tv2两个内部view, view可以给tv1,tv2设置一个touch矩形区域(图中黄色rect1),rect1以view的左上为原点,当手指在rect1产生touch事件时,会触发tv1,tv2的 touch事件,相当于给tv1,tv2增加了另一个touch区域rect1。tv2与rect1部分重合,产生两次回调onTouchListener。

 

注意:必需是外部的view给内部的tv1,tv2设置touchDelegate,见代码红色部分。

代码:

 1 import android.app.Activity;
 2 import android.graphics.Rect;
 3 import android.os.Bundle;
 4 import android.view.MotionEvent;
 5 import android.view.TouchDelegate;
 6 import android.view.View;
 7 import android.widget.TextView;
 8 
 9 public class MainActivity extends Activity {
10 
11     View rootLayout;
12     TouchDelegate delegateTV1,delegateTV2;
13     TextView tv1,tv2;
14     
15     void setTv1Deletegate(){
16         tv1 = (TextView) findViewById(R.id.tv1);
17         
18         Rect delegateRect;
19         delegateRect = new Rect(0, 400, 480, 800);
20         delegateTV1 = new TouchDelegate(delegateRect, tv1);
21         
22         rootLayout.setTouchDelegate(delegateTV1);
23 
24         tv1.setOnTouchListener(new View.OnTouchListener() {
25             @Override
26             public boolean onTouch(View v, MotionEvent event) {
27                 System.out.println(String.format("tv1 touched ,x = %f, y = %f ", event.getX(),event.getY()));
28                 return false;
29             }
30         });
31     }
32     void setTv2Deletegate(){
33         tv2 = (TextView) findViewById(R.id.tv2);
34         View touchRect = findViewById(R.id.touchRect);
35         
36         Rect delegateRect;
37         
38         delegateRect = new Rect(0, -200, 480, 0);//这个rect无效,因为它在touchRect范围之外。
39         
40         delegateTV2 = new TouchDelegate(delegateRect, tv2);
41         
42         touchRect.setTouchDelegate(delegateTV2);
43         
44         tv2.setOnTouchListener(new View.OnTouchListener() {
45             @Override
46             public boolean onTouch(View v, MotionEvent event) {
47                 System.out.println(String.format("tv2 touched ,x = %f, y = %f ", event.getX(),event.getY()));
48                 return false;
49             }
50         });
51     }
52     @Override
53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         setContentView(R.layout.activity_main);
56         rootLayout = findViewById(R.id.activity);
57         
58         setTv1Deletegate();
59         
60         setTv2Deletegate();
61     }
62 }

 

activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/activity"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:gravity="center_horizontal"
 7     android:padding="10dp"
 8     tools:context="com.example.delegatetouch.MainActivity" >
 9 
10     <TextView
11         android:id="@+id/tv1"
12         android:layout_width="match_parent"
13         android:layout_height="100dp"
14         android:background="#d8988e"
15         android:gravity="center_horizontal|center_vertical"
16         android:text="tv1" />
17 
18     <LinearLayout
19         android:layout_width="400dp"
20         android:layout_height="200dp"
21         android:id="@+id/touchRect"
22         android:background="#EEE685"
23         android:layout_alignLeft="@+id/tv1"
24         android:layout_alignParentBottom="true"
25         android:orientation="vertical" >
26 
27         <TextView
28             android:id="@+id/tv2"
29             android:layout_width="200dp"
30             android:layout_height="100dp"
31             android:layout_marginLeft="22dp"
32             android:background="#d8988e"
33             android:gravity="center_horizontal|center_vertical"
34             android:text="tv2" />
35 
36         <TextView
37             android:id="@+id/touchtv"
38             android:layout_width="wrap_content"
39             android:layout_height="wrap_content"
40             android:layout_gravity="center_horizontal"
41             android:text="rect1" />
42 
43     </LinearLayout>
44 
45     <TextView
46         android:id="@+id/view"
47         android:layout_width="wrap_content"
48         android:layout_height="wrap_content"
49         android:layout_above="@+id/touchRect"
50         android:layout_centerHorizontal="true"
51         android:layout_marginBottom="55dp"
52         android:text="view" />
53 
54 </RelativeLayout>

 

posted @ 2016-02-05 21:26  f9q  阅读(351)  评论(0编辑  收藏  举报