Android实现多点触摸的方法
Android应用程序开发中,多点触摸(Multitouch)不是那么遥不可及,实现起来也很简单。如果您对开发多点触摸程序感兴趣的话,那么本文将是一个很好的开始,本例只需要两个类就能实现多点触摸。
首先来看看我们的视图类MTView.java:
1.package com.ideasandroid.demo;
2.
3.t.Context;
4.aphics.Canvas;
5.aphics.Color;
6.aphics.Paint;
7.import android.view.MotionEvent;
8.import android.view.SurfaceHolder;
9.import android.view.SurfaceView;
10.
11.public class MTView extends SurfaceView implements Surface
Holder.Callback {
12.
13. private static final int MAX_TOUCHPOINTS = 10;
14. private static final String START_TEXT = "请随便触摸屏幕
进行测试";
15. private Paint textPaint = new Paint();
16. private Paint touchPaints[] = new Paint[MAX_TOUCHPOINT
S];
17. private int colors[] = new int[MAX_TOUCHPOINTS];
18.
19. private int width, height;
20. private float scale = 1.0f;
21.
22. public MTView(Context context) {
23. super(context);
24. SurfaceHolder holder = getHolder();
25. holder.addCallback(this);
26. setFocusable(true); // 确保我们的View能获得输入焦点
27. setFocusableInTouchMode(true); // 确保能接收到触屏事
件
28. init();
29. }
30.
31. private void init() {
32. // 初始化10个不同颜的画笔
33. textPaint.setColor(Color.WHITE);
34. colors[0] = Color.BLUE;
35. colors[1] = Color.RED;
36. colors[2] = Color.GREEN;
37. colors[3] = Color.YELLOW;
38. colors[4] = Color.CYAN;
39. colors[5] = Color.MAGENTA;
40. colors[6] = Color.DKGRAY;
41. colors[7] = Color.WHITE;
42. colors[8] = Color.LTGRAY;
43. colors[9] = Color.GRAY;
44. for (int i = 0; i <MAX_TOUCHPOINTS; i++) {
45. touchPaints[i] = new Paint();
46. touchPaints[i].setColor(colors[i]);
47. }
48. }
49.
50. /*
51. * 处理触屏事件
52. */
53. @Override
54. public boolean onTouchEvent(MotionEvent event) {
55. // 获得屏幕触点数量
56. int pointerCount = PointerCount();
57. if (pointerCount > MAX_TOUCHPOINTS) {
58.pointerCount = MAX_TOUCHPOINTS;
59. }
60. // 锁定Canvas,开始进行相应的界面处理
61. Canvas c = getHolder().lockCanvas();
62. if (c != null) {
63. c.drawColor(Color.BLACK);
64. if (Action() == MotionEvent.ACTION_UP
) {
65. // 当手离开屏幕时,清屏
66. } else {
67. // 先在屏幕上画一个十字,然后画一个圆
68. for (int i = 0; i <pointerCount; i++) {
69. // 获取一个触点的坐标,然后开始绘制
70. int id = PointerId(i);
71. int x = (int) X(i);
72. int y = (int) Y(i);
73. drawCrosshairsAndText(x, y, touchPaint
s[id], i, id, c);
74. }
75. for (int i = 0; i <pointerCount; i++) {
76. int id = PointerId(i);
77. int x = (int) X(i);
78. int y = (int) Y(i);
79. drawCircle(x, y, touchPaints[id], c);
80. }
81. }
82. // 画完后,unlock
83. getHolder().unlockCanvasAndPost(c);
84. }
85. return true;
86. }
87.
88. /**
89. * 画十字及坐标信息
90. *
91. * @param x
92. * @param y
93. * @param paint
94. * @param ptr
95. * @param id
96. * @param c
97. */
98. private void drawCrosshairsAndText(int x, int y, Paint
paint, int ptr,
99. int id, Canvas c) {
100. c.drawLine(0, y, width, y, paint);
101. c.drawLine(x, 0, x, height, paint);
102. int textY = (int) ((15 + 20 * ptr) * scale); 103. c.drawText("x" + ptr + "=" + x, 10 * scale, textY, textPaint);
104. c.drawText("y" + ptr + "=" + y, 70 * scale, textY, textPaint);
105. c.drawText("id" + ptr + "=" + id, width - 55 * sca le, textY, textPaint);
106. }
107.
108. /**
109. * 画圆
110. *
111. * @param x
112. * @param y
113. * @param paint
114. * @param c
115. */
116. private void drawCircle(int x, int y, Paint paint, Can vas c) {
117. c.drawCircle(x, y, 40 * scale, paint);
118. }
119.
120. /*
121. * 进入程序时背景画成黑,然后把“START_TEXT”写到屏幕
122. */
123. public void surfaceChanged(SurfaceHolder holder, int f ormat, intwidth,
124. int height) {
125.this.width = width;
126.this.height = height;
127. if (width > height) {
128.this.scale = width / 480f;
129. } else {
130.this.scale = height / 480f;
131. }
132. textPaint.setTextSize(14 * scale);
133. Canvas c = getHolder().lockCanvas();
134. if (c != null) {
135. // 背景黑
136. c.drawColor(Color.BLACK);
137. float tWidth = asureText(START_TEX T);
138. c.drawText(START_TEXT, width / 2 - tWidth / 2, height / 2,
139. textPaint);
140. getHolder().unlockCanvasAndPost(c);
141. }
142. }
143.
144. public void surfaceCreated(SurfaceHolder holder) { 145. }
146.
147. public void surfaceDestroyed(SurfaceHolder holder) { 148. }
149.
150.}
151.
接下来看看我们的Activity,MultitouchVisible.java
1.package com.ideasandroid.demo;android平板电脑价格
2.
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.Window;
6.import android.view.WindowManager;
7.
8.public class MultitouchVisible extends Activity {
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. Create(savedInstanceState);
12. //隐藏标题栏
13. requestWindowFeature(Window.FEATURE_NO_TITLE);
14. //设置成全屏
15. getWindow().setFlags(WindowManager.LayoutParams.FL
AG_FULLSCREEN,
16. WindowManager.LayoutParams.FLAG_FULLSCREEN
)
;
17. //设置为上面的MTView
18. setContentView(new MTView(this));
19. }
20.}
希望本文对您有所帮助。
Android智能手机小组资源共享地址(下载更多精彩资料):
-cn/FILE_DOWNLOAD_900004_3000006900.HTM
Android平板电脑小组资源共享地址(下载更多精彩资料):
-cn/FILE_DOWNLOAD_900005_3000006905.HTM
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论