[Android实例] android多点触摸demo

阅读更多
MultiTouchActivity
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类:

package com.zhaokai.multitouch;

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;iMAX_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

你可能感兴趣的:(Android,C,C++,C#,OS)