首页
诗词
字典
板报
句子
名言
友答
励志
学校
网站地图
Android
移动开发
Android
Iphone
Windows Mobile
Symbian
BlackBerry
QT开发
Brew
MeeGo
移动平台
移动软件开发
电信IT应用开发
移动应用
当前位置:
首页
>
教程频道
>
移动开发
>
Android
>
Android开发中兑现多点触摸的方法
2012-06-26
Android开发中实现多点触摸的方法只需要两个类就能实现多点触摸。视图类MTView.java:package com.ideasandr
Android开发中实现多点触摸的方法
只需要两个类就能实现多点触摸。
视图类MTView.java:
package com.ideasandroid.demo;??
import android.content.Context;??
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 MTView extends SurfaceView implements SurfaceHolder.Callback {??
? ? 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 MTView(Context context) {??
? ?? ???super(context);??
? ?? ???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
?= new Paint();??
? ?? ?? ?? ?touchPaints
.setColor(colors
);??
? ?? ???}??
? ? }??
? ? /*??
? ???* 处理触屏事件??
? ???*/??
? ? @Override??
? ? public 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);??
? ?? ?? ?? ?? ? }??
? ?? ?? ?? ?}??
? ?? ?? ?? ?// 画完后,unlock??
? ?? ?? ?? ?getHolder().unlockCanvasAndPost(c);??
? ?? ???}??
? ?? ???return true;??
? ? }??
? ? /**??
? ???* 画十字及坐标信息??
? ???*??
? ???* @param x??
? ???* @param y??
? ???* @param paint??
? ???* @param ptr??
? ???* @param id??
? ???* @param c??
? ???*/??
? ? 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) ((15 + 20 * ptr) * scale);??
? ?? ???c.drawText("x" + ptr + "=" + x, 10 * scale, textY, textPaint);??
? ?? ???c.drawText("y" + ptr + "=" + y, 70 * scale, textY, textPaint);??
? ?? ???c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint);??
? ? }??
? ? /**??
? ???* 画圆??
? ???*??
? ???* @param x??
? ???* @param y??
? ???* @param paint??
? ???* @param c??
? ???*/??
? ? private void drawCircle(int x, int y, Paint paint, Canvas c) {??
? ?? ???c.drawCircle(x, y, 40 * scale, paint);??
? ? }??
? ? /*??
? ???* 进入程序时背景画成黑色,然后把“START_TEXT”写到屏幕??
? ???*/??
? ? public void surfaceChanged(SurfaceHolder holder, int format, intwidth,??
? ?? ?? ?? ?int height) {??
this.width?= width;??
this.height?= height;??
? ?? ???if (width?
>
?height) {??
this.scale?=?width?/ 480f;??
? ?? ???} else {??
this.scale?=?height?/ 480f;??
? ?? ???}??
? ?? ???textPaint.setTextSize(14 * scale);??
? ?? ???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);??
? ?? ???}??
? ? }??
? ? public void surfaceCreated(SurfaceHolder holder) {??
? ? }??
? ? public void surfaceDestroyed(SurfaceHolder holder) {??
? ? }??
}??
接下来看看我们的Activity,MultitouchVisible.java
package com.ideasandroid.demo;??
import android.app.Activity;??
import android.os.Bundle;??
import android.view.Window;??
import android.view.WindowManager;??
public class MultitouchVisible extends Activity {??
? ? @Override??
? ? public void onCreate(Bundle savedInstanceState) {??
? ?? ???super.onCreate(savedInstanceState);??
? ?? ???//隐藏标题栏??
? ?? ???requestWindowFeature(Window.FEATURE_NO_TITLE);??
? ?? ???//设置成全屏??
? ?? ???getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,??
? ?? ?? ?? ?? ? WindowManager.LayoutParams.FLAG_FULLSCREEN);??
? ?? ???//设置为上面的MTView??
? ?? ???setContentView(new MTView(this));??
? ? }??
}?
查看更多
下一篇
本文网址:
https://www.reader8.net/jiaocheng/20120626/1410150.html
读书人精选
热点排行
关于扩展EditText的有关问题
android获得Activity为Theme.Dialog Act
Activity android:launchMode="singleIn
openssl移植Android使用及其相关经验分享
打算开发一个android下的小应用:在这里
android 上的pnglib,zlib jpeglib 库哪
ubuntu11.04下搭建Android推送服务器
android 编译源码 异常解决2
Android上dip、dp、px、sp等组织说明
Android获取所在地城市名二