multitouchactivity
java代码
package com.zhaokai.multitouch;
import android.app.activity;
import android.os.bundle;
import android.view.window;
import android.view.windowmanager;
public class multitouchactivity extends activity {
/** called when the activity is first created. */
/** 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);
//设置为上面的mtview
setcontentview(new mysurfaceview(this));
}
}
surfaceview类:
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.surfaceholder.callback;
import android.view.surfaceview;
public class mysurfaceview extends surfaceview implements 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 mysurfaceview(context context) {
super(context);
surfaceholder holder = getholder();
holder.addcallback(this);
setfocusable(true); // 确保我们的view能获得输入焦点
setfocusableintouchmode(true); // 确保能接收到触屏事件
init();
public 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]);
}
@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){
// 当手离开屏幕时,清屏
c.drawcolor(color.black);
}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);
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) { // 背景黑色
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) {
// todo auto-generated method stub
public void surfacedestroyed(surfaceholder holder) {
}
}