2011.10.17——— android 多点触控

2011.10.17——— android 多点触控
参考: http://blog.csdn.net/ldj299/article/details/6422547

主要API:

event.getPointerCount():触控点的个数
getPointerId(int pointerIndex):pointerIndex从0到getPointerCount-1,返回一个触摸点的标示
getX(int pointerIndex):通过标示来得到X坐标
getY(int pointerIndex):通过标示来得到Y坐标
MotionEvent.ACTION_POINTER_1_DOWN:第一个触摸点点击事件
MotionEvent.ACTION_POINTER_2_DOWN:第二个触摸点点击事件
MotionEvent.ACTION_POINTER_1_UP:第一个触摸点松开事件
MotionEvent.ACTION_POINTER_2_UP:第二个触摸点松开事件


测试代码如下:

自定义view

package com.lp.multi;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View{

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint mPaint = new Paint();
		mPaint.setColor(Color.GRAY);
		mPaint.setTextSize(30);
		canvas.drawText("呵呵", 100, 200, mPaint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int pointCount = event.getPointerCount();
		System.out.println(pointCount);
		if (pointCount == 2) {
			final float x0 = event.getX(event.getPointerId(0));
			final float y0 = event.getY(event.getPointerId(0));

			final float x1 = event.getX(event.getPointerId(1));
			final float y1 = event.getY(event.getPointerId(1));

			switch (event.getAction()) {
				case MotionEvent.ACTION_POINTER_2_DOWN:
					System.out.println("ACTION_POINTER_2_DOWN");
					break;
				case MotionEvent.ACTION_POINTER_1_DOWN:
					System.out.println("ACTION_POINTER_1_DOWN");
					break;
				case MotionEvent.ACTION_POINTER_1_UP:
					System.out.println("ACTION_POINTER_1_UP");
					break;
				case MotionEvent.ACTION_POINTER_2_UP:
					System.out.println("ACTION_POINTER_2_UP");
					break;
				case MotionEvent.ACTION_MOVE: {
					System.out.println("ACTION_MOVE");
					break;
				}
			}
			
			System.out.println(x0+" "+x1+" "+y0+" "+y1);
		}

		return true;
	}
	
	
	

}


注意:
onTouchEvent返回true 要不是没有效果的



Activity
package com.lp.multi;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
}



运行结果:


10-17 17:53:32.269: INFO/System.out(11675): 1
10-17 17:53:32.279: INFO/System.out(11675): 2
10-17 17:53:32.279: INFO/System.out(11675): ACTION_POINTER_2_DOWN
10-17 17:53:32.279: INFO/System.out(11675): 324.3229 201.57916 88.79375 226.62125
10-17 17:53:32.289: INFO/System.out(11675): 2
10-17 17:53:32.289: INFO/System.out(11675): ACTION_MOVE
10-17 17:53:32.299: INFO/System.out(11675): 324.3229 201.57916 88.79375 226.62125
10-17 17:53:32.299: INFO/System.out(11675): 2
10-17 17:53:32.299: INFO/System.out(11675): ACTION_POINTER_1_UP
10-17 17:53:32.309: INFO/System.out(11675): 324.3229 201.57916 88.79375 226.62125
10-17 17:53:32.309: INFO/System.out(11675): 1



结论:

1、ACTION_POINTER_2_DOWN和ACTION_POINTER_1_DOWN 可能只会触发一个
2、事件只触发了一个,但是坐标都是能得到的



你可能感兴趣的:(android)