本文分享了解決surfaceview觸摸軌跡閃爍問題的方法,供大家參考,具體內容如下
第一種解決surfaceview觸摸軌跡閃爍問題的方法:
由于surfaceview使用雙緩存機制,兩張畫布輪流顯示到屏幕上。那么,要存儲觸摸軌跡并避免兩張畫布內容不一致造成的閃爍問題,完全可以利用保存繪制過程并不斷重新繪制的方法解決閃爍,而且這樣還順帶解決了多次試驗中偶爾出現的因為moveto()函數不能讀取到參數執行默認設置(參數設為上次的觸摸點)而出現的斷線連接閃爍問題,詳細代碼如下:
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
|
package com.tobacco.touchdraw; import java.util.arraylist; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.surfaceholder.callback; import android.view.view.ontouchlistener; public class lsurfaceview extends surfaceview implements callback,ontouchlistener,runnable{ private surfaceholder sfh; private canvas canvas; private paint paint; private path path; private arraylist<path> paths; private boolean flag; public lsurfaceview(context context) { super (context); sfh= this .getholder(); sfh.addcallback( this ); paint= new paint(); paint.setcolor(color.red); paint.setantialias( true ); paint.setstrokewidth( 4 ); paint.setstyle(paint.style.stroke); paint.setstrokecap(paint.cap.round); paths= new arraylist<path>(); path= new path(); } public void mydraw(motionevent e){ int action=e.getaction(); switch (action){ case motionevent.action_down: path.moveto(e.getx(),e.gety()); break ; case motionevent.action_move: path.lineto(e.getx(),e.gety()); break ; case motionevent.action_up: //path.close(); path path1= new path(path); paths.add(path1); path.reset(); break ; } } @override public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) { } @override public void surfacecreated(surfaceholder holder) { flag= true ; setontouchlistener( this ); new thread( this ).start(); } @override public void surfacedestroyed(surfaceholder holder) { flag= false ; } @override public boolean ontouch(view v, motionevent event) { mydraw(event); return true ; } @override public void run() { while (flag){ long start=system.currenttimemillis(); canvas=sfh.lockcanvas(); if (canvas!= null ){ canvas.drawcolor(color.black); for ( int i= 0 ;i<paths.size();i++) canvas.drawpath(paths.get(i),paint); canvas.drawpath(path,paint); sfh.unlockcanvasandpost(canvas); } long end=system.currenttimemillis(); try { if (end-start< 30 ){ thread.sleep( 30 -(end-start)); } } catch (exception e){ } } } } |
這里還要注意的是:arraylist保存的是對象的引用,所以要在每次添加時都新建一個對象實體。
第二種解決surfaceview觸摸軌跡閃爍問題的方法:
處理觸屏軌跡的繪制時,用到了surfaceview,建立path對象,在點擊時開始設置path對象,滑動過程中記錄觸摸點,離開后重新設置path對象,因不能阻塞主線程,所以新建了一個子線程來不斷刷新屏幕,也就是將path不斷繪制。但是,接著就出現了一個問題:屏幕中每條軌跡線的終點都會有一小段直線段不斷閃爍。猜測可能是lockcanvas()獲取的對象區域不一樣,就試著使用了lockcanvas(rect re),但是,運行后發現還是沒有解決問題;接著想到可能是因為每次lockcanvas()后獲取的對象不同,就在主線程中添加了一個canvas對象,每次都在canvas對象中修改畫面,然后提交顯示,但是,程序運行后效果絲毫沒有改變!程序代碼如下:
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
|
package com.tobacco.touchdraw; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.surfaceholder.callback; import android.view.view.ontouchlistener; public class mysurfaceview extends surfaceview implements callback,ontouchlistener,runnable{ private surfaceholder sfh; private canvas canvas; private paint paint; private float lastx,lasty; private path path; private boolean flag; public mysurfaceview(context context) { super (context); sfh= this .getholder(); sfh.addcallback( this ); paint= new paint(); paint.setcolor(color.red); paint.setantialias( true ); paint.setstrokewidth( 5 ); paint.setstyle(paint.style.stroke); paint.setstrokecap(paint.cap.round); path= new path(); } public void mydraw(motionevent e){ int action=e.getaction(); switch (action){ case motionevent.action_down: path.moveto(e.getx(),e.gety()); lastx=e.getx(); lasty=e.gety(); break ; case motionevent.action_move: path.quadto(lastx,lasty,e.getx(),e.gety()); lastx=e.getx(); lasty=e.gety(); break ; case motionevent.action_up: //path.quadto(lastx,lasty,e.getx(),e.gety()); path.reset(); break ; } } @override public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) { } @override public void surfacecreated(surfaceholder holder) { flag= true ; setontouchlistener( this ); new thread( this ).start(); } @override public void surfacedestroyed(surfaceholder holder) { flag= false ; } @override public boolean ontouch(view v, motionevent event) { mydraw(event); return true ; } @override public void run() { while (flag){ long start=system.currenttimemillis(); canvas=sfh.lockcanvas(); if (canvas!= null ){ canvas.drawpath(path,paint); sfh.unlockcanvasandpost(canvas); } long end=system.currenttimemillis(); try { if (end-start< 100 ){ thread.sleep( 100 -(end-start)); } } catch (exception e){ } } } } |
以上就是本文的全部內容,希望能夠幫助大家輕松解決surfaceview觸摸軌跡閃爍問題。