Android 多点触摸

原创 2012年03月22日 17:56:05

This is an extremely small and simple app. It only needs two classes which are the Activity and the View.

Here's the Activity, filename MTVisTestActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MTVisTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MTView(this));
}
}

And now the view, filename MTView.java

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 String TAG = "MTView";

private static final int MAX_TOUCHPOINTS = 10;
private static final String START_TEXT = "Touch Anywhere To Test";

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); // make sure we get key events
setFocusableInTouchMode(true); // make sure we get touch events
init();
}

private void init() {
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]);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int pointerCount = event.getPointerCount();
if (pointerCount > MAX_TOUCHPOINTS) {
pointerCount = MAX_TOUCHPOINTS;
}
Canvas c = getHolder().lockCanvas();
if (c != null) {
c.drawColor(Color.BLACK);
if (event.getAction() == MotionEvent.ACTION_UP) {
// clear everything
} else {
// draw crosshairs first then circles as a second pass
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);
}
}
getHolder().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)((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);
}

private void drawCircle(int x, int y, Paint paint, Canvas c) {
c.drawCircle(x, y, 40 * scale, paint);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, 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) {
// clear screen
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) {
}

}

android 2.2 开始的多点触摸示例

  • 2013年05月04日 00:04
  • 13KB
  • 下载

android 2.1 多点触摸演示

  • 2013年04月23日 00:12
  • 54KB
  • 下载

Android多点触摸测试类

package org.wch.gesture; import android.content.Context; import android.graphics.Canvas; impo...

Android多点触摸

  • 2011年03月28日 17:55
  • 84KB
  • 下载

android多点触摸驱动的测试

  • 2011年03月07日 15:55
  • 44KB
  • 下载

Android开发之多点触摸(Multitouch)

如果您对开发多点触摸程序感兴趣的话,那么本文将是一个很好的开始,android应用程序开发中,多点触摸不是那么遥不可及,实现起来也很简单,本例只需要两个类就能实现多点触摸。 首先来看看我们的视图类M...

Android多点触摸交互处理

一)触摸事件监听 监听手机屏幕的触摸事件,通过setOnTouchListener(),系统会回调onTouch(),在这里可以对触摸的不同状态进行监听。 触摸事件onTouch()最后返回值默认...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android 多点触摸
举报原因:
原因补充:

(最多只允许输入30个字)