多點觸控

1 篇文章 / 0 new
author
多點觸控
多點觸控資料取得, 可來自 onTouchEvent()
public class MyView extends View{
...
    @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;
    }    
}
Free Web Hosting