android 事件分发机制2-案例测试


我们来看程序的代码:
要求:
1.通过手指移动来拖动图片
2.控制图片不能超出屏幕显示区域
技术点:
1.MotionEvent处理
2.对View进行动态定位(layout)
package im.weiyuan.com.viewutils; import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnTouchListener{ private ImageView iv_main;
private RelativeLayout parentView;
private int lastX;
private int lastY;
private int maxRight;
private int maxBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_main = (ImageView) findViewById(R.id.iv_main);
parentView = (RelativeLayout) iv_main.getParent();
/*
int right = parentView.getRight(); //0
int bottom = parentView.getBottom(); //0
Toast.makeText(this, right+"---"+bottom, 1).show();
*/
//设置touch监听
iv_main.setOnTouchListener(this); } @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("123456","activity dispatchTouchEvent is called" +ev.getAction());
return super.dispatchTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("123456","activity onTouchEvent is called"+event.getAction());
return super.onTouchEvent(event);
} @Override
public boolean onTouch(View v, MotionEvent event) {
//得到事件的坐标
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//得到父视图的right/bottom
if(maxRight==0) {//保证只赋一次值
maxRight = parentView.getRight();
maxBottom = parentView.getBottom();
}
//第一次记录lastX/lastY
lastX =eventX;
lastY = eventY;
break;
case MotionEvent.ACTION_MOVE:
//计算事件的偏移
int dx = eventX-lastX;
int dy = eventY-lastY;
//根据事件的偏移来移动imageView
int left = iv_main.getLeft()+dx;
int top = iv_main.getTop()+dy;
int right = iv_main.getRight()+dx;
int bottom = iv_main.getBottom()+dy;
//限制left >=0
if(left<0) {
right += -left;
left = 0;
}
//限制top
if(top<0) {
bottom += -top;
top = 0;
}
//限制right <=maxRight
if(right>maxRight) {
left -= right-maxRight;
right = maxRight;
}
//限制bottom <=maxBottom
if(bottom>maxBottom) {
top -= bottom-maxBottom;
bottom = maxBottom;
}
iv_main.layout(left, top, right, bottom);
//再次记录lastX/lastY
lastX = eventX;
lastY = eventY;
break;
default:
break;
}
return true;//所有的motionEvent都交给imageView处理 }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
android 事件分发机制2-案例测试的更多相关文章
- 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...
- 【朝花夕拾】Android自定义View篇之(七)Android事件分发机制(下)滑动冲突解决方案总结
前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/11072989.html],谢谢! 前面两篇文章,花了很大篇幅讲解了Android的事件分发机制 ...
- Android事件分发机制(下)
这篇文章继续讨论Android事件分发机制,首先我们来探讨一下,什么是ViewGroup?它和普通的View有什么区别? 顾名思义,ViewGroup就是一组View的集合,它包含很多的子View和子 ...
- Android事件分发机制(上)
Android事件分发机制这个问题不止一个人问过我,每次我的回答都显得模拟两可,是因为自己一直对这个没有很好的理解,趁现在比较闲对这个做一点总结 举个例子: 你当前有一个非常简单的项目,只有一个Act ...
- android事件分发机制
android事件分发机制,给控件设置ontouch监听事件,当ontouch返回true时,他就不会走onTouchEvent方法,要想走onTouchEvent方法只需要返回ontouch返回fa ...
- [转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分 ...
- Android事件分发机制源码分析
Android事件分发机制源码分析 Android事件分发机制源码分析 Part1事件来源以及传递顺序 Activity分发事件源码 PhoneWindow分发事件源码 小结 Part2ViewGro ...
- 【转】Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9153761 记得在前面的文章中,我带大家一起从源码的角度分析了Android中Vi ...
- 【自己定义控件】android事件分发机制
自己定义Viewgrou中我们或许会常常碰到这种情况,2个子控件的事件冲突导致滑动没实用了.滑动反应非常慢,点击没用了,要划非常多次才移动一点点等等.或许我们第一反应就是百度,google去搜索下答案 ...
随机推荐
- 国际化之fmt标签
1. 什么是国际化和本地化: I. 本地化:一个软件在某个国家或地区使用时,采用该国家或地区的语言,数字,货币,日期等习惯.II. 国际化:软件开发时,让它能支持多个国家和地区的本地化应用.使得应用软 ...
- Could not find the Qt platform plugin windows错误解决方法
在PyCharm中运行PyQt5窗口程序时,出现了下图所有的错误提示. 出现该问题的原因是环境变量没有添加. 解决方法:在环境变量中增加:QT_QPA_PLATFORM_PLUGIN_PATH 路径: ...
- [apue] 一个工业级、跨平台的 tcp 网络服务框架:gevent
作为公司的公共产品,经常有这样的需求:就是新建一个本地服务,产品线作为客户端通过 tcp 接入本地服务,来获取想要的业务能力. 与印象中动辄处理成千上万连接的 tcp 网络服务不同,这个本地服务是跑在 ...
- 关于ueditor编译器
取消自动保存提示.edui-editor-messageholder.edui-default{ visibility:hidden;} Qiyuwen 1033935470@qq.com
- Chisel3 - Tutorial - Adder
https://mp.weixin.qq.com/s/SEcVjGRL1YloGlEPSoHr3A 位数为参数的加法器.通过FullAdder级联实现. 参考链接: https://githu ...
- 【Flume】知识总结
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...
- 自制基于python的DoU log分析脚本
工作中测试DoU的log需要分析,原先是使用excel,去ctrl c,ctrl v截取数据,整理格式等等.一来,这工作虽然很简单,但是非常耗时,不熟练的人(比如我)一搞搞个半天:二来,不小心还会出现 ...
- C# Winform退出程序的方法介绍
这篇文章主要介绍了C#中WinForm程序退出方法, 实例总结了技巧退出WinForm程序窗口的各种常用技巧,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中WinForm程序退出方法技巧 ...
- Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers
[USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...
- Java实现 LeetCode 236 二叉树的最近公共祖先
236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x ...