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 ...
随机推荐
- ●POJ 1329 Circle Through Three Points
题链: http://poj.org/problem?id=1329 题解: 计算几何,求过不共线的三点的圆 就是用向量暴力算出来的东西... (设出外心M的坐标,由于$|\vec{MA}|=|\ve ...
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- k-d树模板(BZOJ2648)
实现了插入一个点,查询距某个位置的最近点. #include <cstdio> #include <algorithm> using namespace std; , inf ...
- JSSDK实现微信自定义分享---java 后端获取签名信息
一.首先说下关于微信Access_token的问题,微信Access_token分为2中: 1.授权token获取方式: 这种token需要code值(如何获取code值查看官方文档) "h ...
- quartz入门详解
http://www.cnblogs.com/monian/p/3822980.html http://www.blogjava.NET/baoyaer/articles/155645.html 另: ...
- Axios 使用文档
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 使用实例:http://www.cnblogs.com/coolslider/p/7838309.ht ...
- 55. Jump Game(中等)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Openlayers3学习心得(初识)
最近刚辞了原来的那家公司,准备新找一份工作.其中有个公司要求会Openlayers3.一看到这个要求,就知道公司业务涉及地图图表比较多. Openlayers本身是一个基于GIS地图相关的功能丰富的J ...
- oclazyload的尝试
https://oclazyload.readme.io/docs http://www.cnblogs.com/BestMePeng/p/AngularJS_ocLazyLoad.html 模块依赖 ...
- 再见,segmentfault
再见,segmentfault 太多的Bug,对segmentfault已经爱不起了. 重回博客园~