2024年3月12日发(作者:)
Android实现多点触摸的方法
Android应用程序开发中,多点触摸(Multitouch)不是那么遥不可及,实
现起来也很简单。如果您对开发多点触摸程序感兴趣的话,那么本文将是一个很
好的开始,本例只需要两个类就能实现多点触摸。
首先来看看我们的视图类:
1. package ;
2.
3. import t;
4. import ;
5. import ;
6. import ;
7. import Event;
8. import eHolder;
9. import eView;
10.
11. public class MTView extends SurfaceView implements Surface
ck {
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. lback(this);
26. setFocusable(true); // 确保我们的View能获得输入焦点
27. setFocusableInTouchMode(true); // 确保能接收到触屏事
件
28. init();
29. }
30.
31. private void init() {
32.
33.
34.
35.
36.
37.
38.
39.
// 初始化10个不同颜色的画笔
or();
colors[0] = ;
colors[1] = ;
colors[2] = ;
colors[3] = ;
colors[4] = ;
colors[5] = A;
40. colors[6] = ;
41. colors[7] = ;
42. colors[8] = ;
43. colors[9] = ;
44. for (int
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
57. if (pointerCount
58.
59. }
60. //
61. Canvas
62. if (c != null) {
63. lor();
64. if (ion() == _UP
) {
65. //
66. } else {
67. //
68. for (int
69. //
70. int
71. int
72. int
i = 0; i < MAX_TOUCHPOINTS; i++) {
处理触屏事件
获得屏幕触点数量
pointerCount = nterCount();
> MAX_TOUCHPOINTS) {
pointerCount = MAX_TOUCHPOINTS;
锁定Canvas,开始进行相应的界面处理
c = getHolder().lockCanvas();
当手离开屏幕时,清屏
先在屏幕上画一个十字,然后画一个圆
i = 0; i < pointerCount; i++) {
获取一个触点的坐标,然后开始绘制
id = nterId(i);
x = (int) (i);
y = (int) (i);
73. drawCrosshairsAndText(x, y, touchPaint
s[id], i, id, c);
74. }
75. for (int i = 0; i < pointerCount; i++) {
76. int id = nterId(i);
77. int x = (int) (i);
78. int y = (int) (i);
79. drawCircle(x, y, touchPaints[id], c);
80. }
81. }
82.
83.
84.
85.
86.
87.
88.
// 画完后,unlock
getHolder().unlockCanvasAndPost(c);
}
return true;
}
/**
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. ne(0, y, width, y, paint);
101. ne(x, 0, x, height, paint);
102. int textY = (int) ((15 + 20 * ptr) * scale);
103. xt("x" + ptr + "=" + x, 10 * scale, textY,
textPaint);
104. xt("y" + ptr + "=" + y, 70 * scale, textY,
textPaint);
105. xt("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. rcle(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. = width;
126. = height;
127. if (width > height) {
128. = width / 480f;
129. } else {
130. = height / 480f;
131. }
132. tSize(14 * scale);
133. Canvas c = getHolder().lockCanvas();
134. if (c != null) {
135. // 背景黑色
136. lor();
137. float tWidth = eText(START_TEX
T);
138. xt(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,
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
package ;
import ty;
import ;
import ;
import Manager;
public class MultitouchVisible extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
12. //隐藏标题栏
13. requestWindowFeature(E_NO_TITLE);
14. //设置成全屏
15. getWindow().setFlags(
AG_FULLSCREEN,
16. _FULLSCREEN
);
17. //设置为上面的MTView
18. setContentView(new MTView(this));
19. }
20.
}
希望本文对您有所帮助。
Android智能手机小组资源共享地址(下载更多精彩资料):
/FILE_DOWNLOAD_900004_
Android平板电脑小组资源共享地址(下载更多精彩资料):
/FILE_DOWNLOAD_900005_


发布评论