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开发时经常会用到,所以非常有必要深入理解它的原理机制. ...
随机推荐
- Ubuntu 下编译安装linux
1. 准备工作切换为管理员权限,sudo –i 输入用户密码 进入root 权限apt-get install build-essential kernel-package libncurses5-d ...
- thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法
随手记录下今天在thinkphp3.2.3中遇到的错误SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.file_info ...
- 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static
在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...
- Portlet之讲解
Portlet在Web门户上管理和显示的可插拔的用户界面组件.Portlet产生可以聚合到门户页面中的标记语言代码的片段,如HTML,XML等.通常,根据桌面隐喻,一个门户页面显示为一组互相不重叠的p ...
- 腾讯开源的轻量级CSS3动画库:JX.Animate
JX.Animate 是由腾讯前端团队 AlloyTeam 推出的一个 CSS3 动画库,通过 JX(腾讯的前端框架)插件的形式提供. Why CSS3 众所周知在支持HTML5的浏览器中 ...
- UNITY_MATRIX_IT_MV[Matrix] (转载)
转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...
- MVC模式在游戏开发的应用
原地址: http://www.cocoachina.com/gamedev/2012/1129/5212.html MVC是三个单词的缩写,分别为:模型(Model).视图(View)和控制Cont ...
- Sqli-labs less 52
Less-52 和less50是一样的,只是这里的mysql错误不会在前台显示,但是对于stacked injection是一样的利用方式 http://127.0.0.1/sqli-labs/Les ...
- 就地交叉数组元素[a1a2b1b2]->[a1b1a2b2]
问题描述: If [a1,a2,a3...,an,b1,b2...bn] is given input change this to [a1,b1,a2,b2.....an,bn] , solutio ...
- 十大技巧优化Android App性能
无论锤子还是茄子手机的不断冒出,Android系统的手机市场占有率目前来说还是最大的,因此基于Android开发的App数量也是很庞大的. 那么,如何能开发出更高性能的Android App?相信是软 ...