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,见代码红色部分。
代码:
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的更多相关文章
- Android Touch(2)View.OnTouchEvent与View.OnTouchListener区别
1,在官方文档 docs/reference/android/view/View.OnTouchListener.html 中对OnTouchListener的描述 Interface definit ...
- Android Touch事件传递机制全面解析(从WMS到View树)
转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...
- Android Touch事件传递机制 二:单纯的(伪生命周期)
转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...
- Android Touch事件传递机制 一: OnTouch,OnItemClick(监听器),dispatchTouchEvent(伪生命周期)
ViewGroup View Activity dispatchTouchEvent 有 有 有 onInterceptTouchEvent 有 无 无 onTouchEvent 有 有 有 例 ...
- Android touch 事件传递机制
前言: (1)在自定义view的时候经常会遇到事件拦截处理,比如在侧滑菜单的时候,我们希望在侧滑菜单里面有listview控件,但是我们希望既能左右滑动又能上下滑动,这个时候就需要对触摸的touch事 ...
- Android touch事件处理流程
前面我们看了key事件的处理流程,相信大家对此已经有了新的认识,这篇文章我打算带领大家来看看稍微复杂些的touch 事件的处理流程.说它复杂是因为key事件本身就key down,up,long pr ...
- Android Touch事件传递机制解析 (推荐)
最近新闻列表里的下拉 down up move 等等让我十分头疼 ,无意间看到了一篇非常不错的帖子,转载如下: 开篇语:最近程序在做一个小效果,要用到touch,结果整得云里面雾里的,干脆就好好把a ...
- Android Touch事件传递机制通俗讲解
在讲正题之前我们讲一段有关任务传递的小故事,抛砖迎玉下: 话说一家软件公司,来一个任务,分派给了开发经理去完成: 开发经理拿到,看了一下,感觉好简单,于是 开发经理:分派给了开发组长 开发组长:分派给 ...
- Android Touch事件原理加实例分析
Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...
随机推荐
- Spring Junit4 Test
捣鼓了差不多一天...终于把"No Session found for current thread"问题解决了 环境:Spring 4.0.6 RELEASE + Hiberna ...
- .NET设计模式(19):观察者模式(Observer Pattern)(转)
概述 在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” ——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知.如果这样的依赖关系过于紧密,将使软件不能很好地抵御 ...
- ASP.NET防止用户多次登录的方法
常见的处理方法是,在用户登录时,判断此用户是否已经在Application中存在,如果存在就报错,不存在的话就加到Application中(Application是所有Session共有的,整个web ...
- ASP.NET状态管理详解,让你明明白白
开发WinFrom的程序员可能不会在意维护应用程序的状态,因为WinFrom本身就在客户端运行,可以直接在内存中维护其应用程序状态.但ASP.NET应用程序在服务器端运行,客户端使用无状态的http协 ...
- 从浏览器的console获取angularjs的scope
http://ionicframework.com/blog/angularjs-console/ 1: Access Scopes We can access any scope (even iso ...
- Static、final、abstract、接口、构造方法及java语法总结
Static:定义类的时候一般不用static来修饰,在一定意义上,用static修饰的字段可以作为全局变量,static修饰的字段和方法存储在类的内存区域,所有实例共享.static字段和方法都是属 ...
- leetcode course shedule
题目就不说了,问题本质就是在一个有向图中查找它是不是存在环. 上网百度了一下,方法是,找出图中入度为0 的点,将以它为起点的边去掉. 重复这一动作,直到所有的边都被去掉(没有环)或者存在边但是无法再去 ...
- (转)Eclipse平台技术概述
转载:周金根 http://zhoujg.blog.51cto.com/1281471/516833 Eclipse:Eclipse平台技术概述 2010-10-19 13:35:00 标签:E ...
- uva 11090
I I U P C 2 0 0 6 Problem G: Going in Cycle!! Input: standard input Output: standard output You are ...
- 深入浅出ES6(十六):模块 Modules
作者 Jason Orendorff github主页 https://github.com/jorendorff 早在2007年我刚加入Mozilla的JavaScript团队的时候广为流传一个 ...