最近没什么事看了一下多点触控的例子,跟我开始想的实现方法一样,只是一些函数不知道:下面是常用的函数解释(copy过来滴....)
event.getAction() //获取触控动作比如ACTION_DOWN
event.getPointerCount(); //获取触控点的数量,比如2则可能是两个手指同时按压屏幕
event.getPointerId(nID); //对于每个触控的点的细节,我们可以通过一个循环执行getPointerId方法获取索引
event.getX(nID); //获取第nID个触控点的x位置
event.getY(nID); //获取第nID个点触控的y位置
event.getPressure(nID); //LCD可以感应出用户的手指压力,当然具体的级别由驱动和物理硬件决定的
event.getDownTime() //按下开始时间
event.getEventTime() // 事件结束时间
event.getEventTime()-event.getDownTime()); //总共按下时花费时间
下面是一个例子,其实很简单,不过是拷贝过来的,放在这里可以备忘,以后的学习.......:
代码如下:
package com.xy.multitouchvisible;import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;public class MyView extends SurfaceView implements SurfaceHolder.Callback {MainActivity father;private static final int MAX_TOUCHPOINTS = 10;private static final String START_TEXT = "请随便触摸屏幕进行测试";private Paint textPaint = new Paint();private Paint touchPaints[] = new Paint[MAX_TOUCHPOINTS];private int colors[] = new int[MAX_TOUCHPOINTS];private int width, height;// private float scale = 1.0f;public MyView(MainActivity father) {super(father);// TODO Auto-generated constructor stubthis.father = father;SurfaceHolder holder = getHolder();holder.addCallback(this);setFocusable(true); // 确保我们的View能获得输入焦点setFocusableInTouchMode(true); // 确保能接收到触屏事件init();}private void init() {// 初始化10个不同颜色的画笔textPaint.setColor(Color.WHITE);colors[0] = Color.BLUE;colors[1] = Color.RED;colors[2] = Color.GREEN;colors[3] = Color.YELLOW;colors[4] = Color.CYAN;colors[5] = Color.MAGENTA;colors[6] = Color.DKGRAY;colors[7] = Color.WHITE;colors[8] = Color.LTGRAY;colors[9] = Color.GRAY;for (int i = 0; i < MAX_TOUCHPOINTS; i++) {touchPaints[i] = new Paint();touchPaints[i].setColor(colors[i]);}}/** 处理触屏事件*/@Overridepublic boolean onTouchEvent(MotionEvent event) {// 获得屏幕触点数量int pointerCount = event.getPointerCount();if (pointerCount > MAX_TOUCHPOINTS) {pointerCount = MAX_TOUCHPOINTS;}// 锁定Canvas,开始进行相应的界面处理Canvas c = getHolder().lockCanvas();if (c != null) {c.drawColor(Color.BLACK);if (event.getAction() == MotionEvent.ACTION_UP) {// 当手离开屏幕时,清屏} else {// 先在屏幕上画一个十字,然后画一个圆for (int i = 0; i < pointerCount; i++) {// 获取一个触点的坐标,然后开始绘制int id = event.getPointerId(i);int x = (int) event.getX(i);int y = (int) event.getY(i);drawCrosshairsAndText(x, y, touchPaints[id], i, id, c);}for (int i = 0; i < pointerCount; i++) {int id = event.getPointerId(i);int x = (int) event.getX(i);int y = (int) event.getY(i);drawCircle(x, y, touchPaints[id], c);}}// 画完后,unlockgetHolder().unlockCanvasAndPost(c);}return true;}/*** 画十字及坐标信息*/private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr,int id, Canvas c) {c.drawLine(0, y, width, y, paint);c.drawLine(x, 0, x, height, paint);int textY = (int) (5 + 20 * ptr);c.drawText("x" + ptr + "=" + x, 10, textY, textPaint);c.drawText("y" + ptr + "=" + y, 70, textY, textPaint);c.drawText("id" + ptr + "=" + id, width - 55, textY, textPaint);}/*** 画圆*/private void drawCircle(int x, int y, Paint paint, Canvas c) {c.drawCircle(x, y, 40, paint);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stubthis.width = width;this.height = height;textPaint.setTextSize(14);Canvas c = getHolder().lockCanvas();if (c != null) {// 背景黑色c.drawColor(Color.BLACK);float tWidth = textPaint.measureText(START_TEXT);c.drawText(START_TEXT, width / 2 - tWidth / 2, height / 2,textPaint);getHolder().unlockCanvasAndPost(c);}}@Overridepublic void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub}
}用真机测试采用感觉哦.........