Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android開發中實現多點觸摸的方法

Android開發中實現多點觸摸的方法

編輯:高級開發

我們曾就《android手勢識別VIEwFlipper觸摸動畫》做過詳細的講解,其實,android應用程序開發中,多點觸摸(Multitouch)不是那麼遙不可及,實現起來也很簡單。如果您對開發多點觸摸程序感興趣的話,那麼本文將是一個很好的開始,本例只需要兩個類就能實現多點觸摸。

首先來看看我們的視圖類MTVIEw.Java:

  1. package com.ideasandroid.demo;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.vIEw.MotionEvent;
  7. import android.vIEw.SurfaceHolder;
  8. import android.view.SurfaceVIEw;
  9. public class MTView extends SurfaceVIEw implements SurfaceHolder.Callback {
  10. private static final int MAX_TOUCHPOINTS = 10;
  11. private static final String START_TEXT = "請隨便觸摸屏幕進行測試";
  12. private Paint textPaint = new Paint();
  13. private Paint touchPaints[] = new Paint[MAX_TOUCHPOINTS];
  14. private int colors[] = new int[MAX_TOUCHPOINTS];
  15. private int width, height;
  16. private float scale = 1.0f;
  17. public MTVIEw(Context context) {
  18. super(context);
  19. SurfaceHolder holder = getHolder();
  20. holder.addCallback(this);
  21. setFocusable(true); // 確保我們的VIEw能獲得輸入焦點
  22. setFocusableInTouchMode(true); // 確保能接收到觸屏事件
  23. init();
  24. }
  25. private void init() {
  26. // 初始化10個不同顏色的畫筆
  27. textPaint.setColor(Color.WHITE);
  28. colors[0] = Color.BLUE;
  29. colors[1] = Color.RED;
  30. colors[2] = Color.GREEN;
  31. colors[3] = Color.YELLOW;
  32. colors[4] = Color.CYAN;
  33. colors[5] = Color.MAGENTA;
  34. colors[6] = Color.DKGRAY;
  35. colors[7] = Color.WHITE;
  36. colors[8] = Color.LTGRAY;
  37. colors[9] = Color.GRAY;
  38. for (int i = 0; i < MAX_TOUCHPOINTS; i++) {
  39. touchPaints[i] = new Paint();
  40. touchPaints[i].setColor(colors[i]);
  41. }
  42. }
  43. /*
  44. * 處理觸屏事件
  45. */
  46. @Override
  47. public boolean onTouchEvent(MotionEvent event) {
  48. // 獲得屏幕觸點數量
  49. int pointerCount = event.getPointerCount();
  50. if (pointerCount > MAX_TOUCHPOINTS) {
  51. pointerCount = MAX_TOUCHPOINTS;
  52. }
  53. // 鎖定Canvas,開始進行相應的界面處理
  54. Canvas c = getHolder().lockCanvas();
  55. if (c != null) {
  56. c.drawColor(Color.BLACK);
  57. if (event.getAction() == MotionEvent.ACTION_UP) {
  58. // 當手離開屏幕時,清屏
  59. } else {
  60. // 先在屏幕上畫一個十字,然後畫一個圓
  61. for (int i = 0; i < pointerCount; i++) {
  62. // 獲取一個觸點的坐標,然後開始繪制
  63. int id = event.getPointerId(i);
  64. int x = (int) event.getX(i);
  65. int y = (int) event.getY(i);
  66. drawCrosshairsAndText(x, y, touchPaints[id], i, id, c);
  67. }
  68. for (int i = 0; i < pointerCount; i++) {
  69. int id = event.getPointerId(i);
  70. int x = (int) event.getX(i);
  71. int y = (int) event.getY(i);
  72. drawCircle(x, y, touchPaints[id], c);
  73. }
  74. }
  75. // 畫完後,unlock
  76. getHolder().unlockCanvasAndPost(c);
  77. }
  78. return true;
  79. }
  80. /**
  81. * 畫十字及坐標信息
  82. *
  83. * @param x
  84. * @param y
  85. * @param paint
  86. * @param ptr
  87. * @param id
  88. * @param c
  89. */
  90. private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr,
  91. int id, Canvas c) {
  92. c.drawLine(0, y, width, y, paint);
  93. c.drawLine(x, 0, x, height, paint);
  94. int textY = (int) ((15 + 20 * ptr) * scale);
  95. c.drawText("x" + ptr + "=" + x, 10 * scale, textY, textPaint);
  96. c.drawText("y" + ptr + "=" + y, 70 * scale, textY, textPaint);
  97. c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint);
  98. }
  99. /**
  100. * 畫圓
  101. *
  102. * @param x
  103. * @param y
  104. * @param paint
  105. * @param c
  106. */
  107. private void drawCircle(int x, int y, Paint paint, Canvas c) {
  108. c.drawCircle(x, y, 40 * scale, paint);
  109. }
  110. /*
  111. * 進入程序時背景畫成黑色,然後把“START_TEXT”寫到屏幕
  112. */
  113. public void surfaceChanged(SurfaceHolder holder, int format, intwidth,
  114. int height) {
  115. this.width = width;
  116. this.height = height;
  117. if (width > height) {
  118. this.scale = width / 480f;
  119. } else {
  120. this.scale = height / 480f;
  121. }
  122. textPaint.setTextSize(14 * scale);
  123. Canvas c = getHolder().lockCanvas();
  124. if (c != null) {
  125. // 背景黑色
  126. c.drawColor(Color.BLACK);
  127. float tWidth = textPaint.measureText(START_TEXT);
  128. c.drawText(START_TEXT, width / 2 - tWidth / 2, height / 2,
  129. textPaint);
  130. getHolder().unlockCanvasAndPost(c);
  131. }
  132. }
  133. public void surfaceCreated(SurfaceHolder holder) {
  134. }
  135. public void surfaceDestroyed(SurfaceHolder holder) {
  136. }
  137. }

接下來看看我們的Activity,MultitouchVisible.Java

  1. package com.ideasandroid.demo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.vIEw.Window;
  5. import android.vIEw.WindowManager;
  6. public class MultitouchVisible extends Activity {
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. //隱藏標題欄
  11. requestWindowFeature(Window.FEATURE_NO_TITLE);
  12. //設置成全屏
  13. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  14. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  15. //設置為上面的MTVIEw
  16. setContentView(new MTVIEw(this));
  17. }
  18. }

希望本文對您有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved