Android触摸屏幕时间-android学习之旅(三)
android的多点触摸是经常遇到的编程技巧,这一篇可以将详细的介绍这个问题。
简单实例
android的触摸需要实现OnTouchListener接口,继承里面方法。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
java代码;
public class MainActivity extends Activity {
private FrameLayout frame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frame = (FrameLayout) findViewById(R.id.frame);
frame.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("down");
break;
case MotionEvent.ACTION_UP:
System.out.println("up");
break;
case MotionEvent.ACTION_MOVE:
System.out.println("move");
break;
default:
break;
}
return false;
}
});
}
}
事件的传递
注意上面的方法的返回的布尔值,代表该触屏事件是否成功,如果不成功不会继续下一个触屏事件。
上面的ACTION_DOWN是要收放在屏幕上触发,ACTION_MOVE是手指在屏幕上移动时候触发,ACTION_UP是离开屏幕时候触发他们事件会有一个逻辑的先后顺序,如果像上面那样。返回值为false只会触发ACTION_DOWN不会触发后面的方法,所以要改为return true;
android机器人随着鼠标移动的实例
本例中出现了LayoutParams这个类。这个类是要向父类布局,说明子类控件的位置。
首先获取触摸点的位置,然后设置imageView控件的位置,正好与触摸点重合,就造成了图像随着鼠标移动的效果。
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/ic_launcher"/>
</FrameLayout>
</LinearLayout>
java代码
public class MainActivity extends Activity {
private FrameLayout frame;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frame = (FrameLayout) findViewById(R.id.frame);
image = (ImageView) findViewById(R.id.image);
frame.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("down");
break;
case MotionEvent.ACTION_UP:
System.out.println("up");
FrameLayout.LayoutParams lp = (LayoutParams) image.getLayoutParams();
lp.leftMargin = (int)event.getX();
lp.rightMargin = (int)event.getY();
image.setLayoutParams(lp);
System.out.println(String.format("x:%f,y:%f", event.getX(),event.getY()));
break;
case MotionEvent.ACTION_MOVE:
System.out.println("move");
break;
default:
break;
}
return false;
}
});
}
}
效果图
Android触摸屏幕时间-android学习之旅(三)的更多相关文章
- Android万能适配器Adapter-android学习之旅(74)
万能适配器的代码的github地址是https://github.com/fengsehng/CommonAdapter 万能适配器的代码的github地址是https://github.com/fe ...
- Android自制浏览器WebView-android学习之旅(64)
简单讲解如何使用WebView加载百度的网页 acticity代码 public class MainActivity extends Activity { private WebView webVi ...
- 滴滴Booster移动APP质量优化框架 学习之旅 三
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) 滴滴Booster移动App质量优化框架-学习之旅 二对重复资源 ...
- Hadoop学习之旅三:MapReduce
MapReduce编程模型 在Google的一篇重要的论文MapReduce: Simplified Data Processing on Large Clusters中提到,Google公司有大量的 ...
- Android开发艺术探索学习笔记(三)
第三章 View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...
- 【Android群英传】学习笔记(三·一)
本篇笔记中,笔者将记录在ListView的使用的技巧 虽然5.X时代,RecyclerView在很多地方都在逐渐取代ListView,但ListView的使用范围还是很广泛的,它这万年老大哥的地位也不 ...
- Android studio使用git-android学习之旅(79)
首先我参考了hello_my_show和梦痕_sky的博客,表示感谢 android studio对于git的支持是很好的,这节课我们拉讲解怎么使用git可视化工具来clone project和提交修 ...
- Android为什么使用Binder-android学习之旅(101)
基础知识 Android进程和linux进程一样,他们只运行在进程固有的虚拟空间中.一个4GB的虚拟地址空间,其中3GB是用户空间,1GB是内核空间 ,用户空间是非共享的,内核空间是共享的,如下图: ...
- Android首选项SharedPreference-android学习之旅(六)
SharedPrefenence采用的键值对的方式来进行存储,采用内部存储的方式. 实例 public class MainActivity extends Activity { private Sh ...
随机推荐
- Codeforces April Fools Contest 2017
都是神题,我一题都不会,全程听学长题解打代码,我代码巨丑就不贴了 题解见巨神博客 假装自己没有做过这套
- [BZOJ]4644: 经典傻逼题
某天我觉得一切题目都是那么不可做,于是百度了一下"傻逼题"-- 题目大意:对于图中的任意一个点集(可以为空或者全集),所有恰好有一个端点在这个点集中的边组成的集合被称为割.一个割的 ...
- ●BZOJ 4698 Sdoi2008 Sandy的卡片
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4698 题解: 后缀数组,二分这个题还是比较套路的.首先依据题意,把各个串差分以后,用分割符号 ...
- 勤拂拭软件 java web 开发教程(1) - 开发环境搭建
勤拂拭软件系列教程 之 Java Web开发之旅(1) Java Web开发环境搭建 1 前言 工作过程中,遇到不少朋友想要学习jsp开发,然而第一步都迈不出,连一个基本的环境都没有,试问,如何能够继 ...
- Mysql 基于GTID的主从复制(实操)
实现环境: Master 主:192.168.0.102 (Mysql 5.6.36) Slave 从 :192.168.0.103 (Mysql 5.6.36) 步骤1.在主DB服务器上建立复制账 ...
- cocos2d-x-3.0beta2创建项目遇到“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)”的问题
在Windows平台下 用cocos2d-x-3.0beta2版本下的create_project.py工具创建项目 但是遇到如下问题:UnicodeDecodeError: 'ascii' code ...
- PHP内核之旅-1.生命周期
1.SAPI接口 PHP具体应用的编程接口. 2.开始和结束 PHP开始执行以后会经过两个主要的阶段: 处理请求之前的开始阶段和请求之后的结束阶段. 1.1开始阶段: 1.1.1 模块初始化阶段(MI ...
- js 当前时间刷新
<p>每隔1秒钟,打印当前时间</p> <div id="time"></div> <script> function ...
- jQuery ajax中使用serialize()方法提交表单数据示例
<form id="form"> 输入账号 :<input id="name" type="text" name=&quo ...
- angular-cli学习笔记 快速创建代码模板
组件: ng g component component/demo 服务: ng g service service/news 然后在app.module.ts里引入 ng g service ser ...