作用:

基类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,见代码红色部分。

代码:

 import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.TextView; public class MainActivity extends Activity { View rootLayout;
TouchDelegate delegateTV1,delegateTV2;
TextView tv1,tv2; void setTv1Deletegate(){
tv1 = (TextView) findViewById(R.id.tv1); Rect delegateRect;
delegateRect = new Rect(, , , );
delegateTV1 = new TouchDelegate(delegateRect, tv1); rootLayout.setTouchDelegate(delegateTV1); tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println(String.format("tv1 touched ,x = %f, y = %f ", event.getX(),event.getY()));
return false;
}
});
}
void setTv2Deletegate(){
tv2 = (TextView) findViewById(R.id.tv2);
View touchRect = findViewById(R.id.touchRect); Rect delegateRect; delegateRect = new Rect(, -, , );//这个rect无效,因为它在touchRect范围之外。 delegateTV2 = new TouchDelegate(delegateRect, tv2); touchRect.setTouchDelegate(delegateTV2); tv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println(String.format("tv2 touched ,x = %f, y = %f ", event.getX(),event.getY()));
return false;
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout = findViewById(R.id.activity); setTv1Deletegate(); setTv2Deletegate();
}
}

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="10dp"
tools:context="com.example.delegatetouch.MainActivity" > <TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#d8988e"
android:gravity="center_horizontal|center_vertical"
android:text="tv1" /> <LinearLayout
android:layout_width="400dp"
android:layout_height="200dp"
android:id="@+id/touchRect"
android:background="#EEE685"
android:layout_alignLeft="@+id/tv1"
android:layout_alignParentBottom="true"
android:orientation="vertical" > <TextView
android:id="@+id/tv2"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:background="#d8988e"
android:gravity="center_horizontal|center_vertical"
android:text="tv2" /> <TextView
android:id="@+id/touchtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="rect1" /> </LinearLayout> <TextView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/touchRect"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:text="view" /> </RelativeLayout>

Android Touch(3)View的touchDelegate的更多相关文章

  1. Android Touch(2)View.OnTouchEvent与View.OnTouchListener区别

    1,在官方文档 docs/reference/android/view/View.OnTouchListener.html 中对OnTouchListener的描述 Interface definit ...

  2. Android Touch事件传递机制全面解析(从WMS到View树)

    转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...

  3. Android Touch事件传递机制 二:单纯的(伪生命周期)

    转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...

  4. Android Touch事件传递机制 一: OnTouch,OnItemClick(监听器),dispatchTouchEvent(伪生命周期)

      ViewGroup View  Activity dispatchTouchEvent 有 有 有 onInterceptTouchEvent 有 无 无 onTouchEvent 有 有 有 例 ...

  5. Android touch 事件传递机制

    前言: (1)在自定义view的时候经常会遇到事件拦截处理,比如在侧滑菜单的时候,我们希望在侧滑菜单里面有listview控件,但是我们希望既能左右滑动又能上下滑动,这个时候就需要对触摸的touch事 ...

  6. Android touch事件处理流程

    前面我们看了key事件的处理流程,相信大家对此已经有了新的认识,这篇文章我打算带领大家来看看稍微复杂些的touch 事件的处理流程.说它复杂是因为key事件本身就key down,up,long pr ...

  7. Android Touch事件传递机制解析 (推荐)

    最近新闻列表里的下拉 down up  move 等等让我十分头疼 ,无意间看到了一篇非常不错的帖子,转载如下: 开篇语:最近程序在做一个小效果,要用到touch,结果整得云里面雾里的,干脆就好好把a ...

  8. Android Touch事件传递机制通俗讲解

    在讲正题之前我们讲一段有关任务传递的小故事,抛砖迎玉下: 话说一家软件公司,来一个任务,分派给了开发经理去完成: 开发经理拿到,看了一下,感觉好简单,于是 开发经理:分派给了开发组长 开发组长:分派给 ...

  9. Android Touch事件原理加实例分析

    Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...

随机推荐

  1. ngcordova 监控网络制式改变

    ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...

  2. 【BZOJ】【1048】【HAOI2007】分割矩阵

    DP/记忆化搜索 暴力枚举分割方案?……大概是指数级的?大约是20!的方案= =? 但是我们看到a.b.n的范围都很小……所以不同的状态数只是$10^5$级别的,可以记忆化搜索求解 比较水的一道题…… ...

  3. 【BZOJ】【1031】【JSOI2007】字符加密Cipher

    后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们 ...

  4. SimpleDateFormat

    范例: Date date = new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd,E,HH. ...

  5. matrix_last_acm_4

    2013 ACM-ICPC吉林通化全国邀请赛 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97654#problem/A 题意:输入 ...

  6. Swift-5-流程控制

    // Playground - noun: a place where people can play import UIKit // For-In 循环 // 1 遍历数字区间 ... { prin ...

  7. 连接ACCESS 数据库不能使用 '';文件已在使用中。

    错误类型:  Microsoft JET Database Engine (0x80004005)  不能使用 '':文件已在使用中. 对数据库的操作完之后,要 conn.close() 错误原因:解 ...

  8. spring 两个 properties

    A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件: A模块 A模块的Spring配置文件如下: <?xml version="1.0" enc ...

  9. Unity 处理策划的 Excel

    很多时候我们需要使用策划的Excel表来做游戏的静态数据配置, 而不是采用自己定义的xml或者U3D的scriptobject. 因为很多数据都是策划处理的,而策划最喜欢的就是excel,也只会用这个 ...

  10. MySQL数据库数据类型之集合类型SET测试总结

    MySQL数据库提供针对字符串存储的一种特殊数据类型:集合类型SET,这种数据类型可以给予我们更多提高性能.降低存储容量和降低程序代码理解的技巧,前面介绍了首先介绍了四种数据类型的特性总结,其后又分别 ...