本文实例介绍了android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下
效果图:
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
package com.android.gameview5; import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.os.bundle; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceholder.callback; import android.view.surfaceview; import android.view.window; import android.view.windowmanager; public class surfaceviewactivity3 extends activity { public void oncreate(bundle s){ super .oncreate(s); //全屏显示 requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview( new myview( this )); } public class myview extends surfaceview implements callback,runnable{ public static final int time_in_frame = 50 ; paint mpaint = null ; paint mtextpaint = null ; surfaceholder msurfaceholder = null ; boolean mrunning = false ; canvas mcanvas = null ; private path mpath; private float mposx,mposy; public myview(context context){ super (context); this .setfocusable( true ); this .setfocusableintouchmode( true ); msurfaceholder = this .getholder(); msurfaceholder.addcallback( this ); mcanvas = new canvas(); mpaint = new paint(); mpaint.setcolor(color.black); mpaint.setantialias( true ); mpaint.setstyle(paint.style.stroke); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth( 6 ); mpath = new path(); mtextpaint = new paint(); mtextpaint.setcolor(color.black); mtextpaint.settextsize( 15 ); } public boolean ontouchevent(motionevent event){ int action = event.getaction(); float x = event.getx(); float y = event.gety(); switch (action){ case motionevent.action_down: mpath.moveto(x, y); break ; case motionevent.action_move: mpath.quadto(mposx, mposy, x, y); break ; case motionevent.action_up: //mpath.reset(); break ; } //记录当前触摸点得当前得坐标 mposx = x; mposy = y; return true ; } private void ondraw(){ mcanvas.drawcolor(color.white); //绘制曲线 mcanvas.drawpath(mpath, mpaint); mcanvas.drawtext( "当前触笔x:" +mposx, 0 , 20 ,mtextpaint); mcanvas.drawtext( "当前触笔y:" +mposy, 0 , 40 ,mtextpaint); } public void run() { // todo auto-generated method stub while (mrunning){ long starttime = system.currenttimemillis(); synchronized (msurfaceholder){ mcanvas = msurfaceholder.lockcanvas(); ondraw(); msurfaceholder.unlockcanvasandpost(mcanvas); } long endtime = system.currenttimemillis(); int difftime = ( int ) (endtime - starttime); while (difftime<=time_in_frame){ difftime =( int )(system.currenttimemillis()-starttime); thread.yield(); } } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub } @override public void surfacecreated(surfaceholder holder) { mrunning = true ; new thread( this ).start(); } @override public void surfacedestroyed(surfaceholder holder) { // todo auto-generated method stub mrunning = false ; } } } |
以上就是android轻松画出触摸轨迹的具体方法,希望对大家的学习有所帮助。