Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 多點觸摸(Multitouch)實現方法詳解

編輯:關於 Android 程式設計

多點觸摸並不像許多人想像的那樣遙不可及。在 Android 中,借助 MotionEvent 提供的觸點信息,只需兩個類就能實現一個能同時追蹤多根手指的演示程序。本文以一個可在屏幕上繪製彩色圓點與十字準線的例子,帶大家直觀感受多點觸摸的實現過程。

一、自定義視圖 MTView

核心思路是繼承 SurfaceView,在 onTouchEvent 中讀取當前觸點數量與每個觸點的坐標,再用不同顏色的畫筆把手指位置畫出來。

public class MTView extends SurfaceView implements SurfaceHolder.Callback {
    private static final int MAX_TOUCHPOINTS = 10;
    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);
        getHolder().addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true); // 確保能接收觸屏事件
        init();
    }

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

二、處理觸屏事件

關鍵在於 event.getPointerCount() 取得觸點數量,再用 getPointerId(i) 拿到每根手指的 id,配合 getX(i)、getY(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) {
            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);
                drawCircle((int) event.getX(i), (int) event.getY(i), 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, paint);
    c.drawText("y" + ptr + "=" + y, 70 * scale, textY, paint);
    c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, paint);
}

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

三、全屏 Activity

最後在 Activity 中隱藏標題欄、設置全屏,並把 MTView 作為內容視圖即可:

public class MultitouchVisible extends Activity {
    @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));
    }
}

可以看到,多點觸摸的關鍵就是 MotionEvent 中的 pointerCount、getX(i)/getY(i) 這組按索引取值的方法。理解了這套機制,無論是手勢縮放還是多人協作繪圖,都能在此基礎上靈活擴展。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved