首页 技术 正文
技术 2022年11月9日
0 收藏 317 点赞 5,120 浏览 2150 个字

下面是一个两点触控的案例代码:

package com.zzj;import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;public class AndroidTestActivity extends Activity {
private float x0, y0;
private float x1, y1; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public boolean onTouchEvent(MotionEvent event) {
int pointerCount = event.getPointerCount();
int action = event.getAction();
if (pointerCount == 1) {
switch (action) {
case MotionEvent.ACTION_DOWN:
x0 = event.getX(0);
y0 = event.getY(0);
System.out.println("ACTION_DOWN pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE pointerCount=" + pointerCount);
break;
}
}
if (pointerCount == 2) {
switch (action) {
case MotionEvent.ACTION_DOWN:
x0 = event.getX(0);
y0 = event.getY(0);
System.out.println("ACTION_DOWN pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_MOVE:
System.out.println("ACTION_MOVE pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_POINTER_1_DOWN:
System.out.println("ACTION_POINTER_1_DOWN pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_POINTER_1_UP:
System.out.println("ACTION_POINTER_1_UP pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
System.out.println("ACTION_POINTER_2_DOWN pointerCount=" + pointerCount);
break;
case MotionEvent.ACTION_POINTER_2_UP:
System.out.println("ACTION_POINTER_2_UP pointerCount=" + pointerCount);
break;
}
}
return super.onTouchEvent(event);
}
}

下面是该案例的一些分析要点:1)使用event.getPointerCount()来获取当前触控点的个数。并判断触控点,来分别对不同的点进行事件处理。

                2)使用event.getAction()来获取当前的事件码,单点按下、松开和移动的事件分别是:MotionEvent.ACTION_DOWN、ACTION_UP、ACTION_MOVE;第二个点按下、松开和移动的事件分别是ACTION_POINTER_2_DOWN、                                    ACTION_POINTER_2_UP、ACTION_MOVE,注意,单点和两点响应相同的移动事件,即ACTION_MOVE。

                3)只有第二个点按下之后,才会响应MotionEvent.ACTION_POINTER_1_DOWN、MotionEvent.ACTION_POINTER_1_UP,初次单击是不响应这两个事件码的。这点切记。

                4)通过event.getX(0)、event.getY(0)来获取第一个点的坐标值,通过event.getX(1)、event.getY(1)来获取第二个点的坐标值。如果有更多的点,依次类推。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295