本文實(shí)例講述了android編程之surfaceview用法。分享給大家供大家參考,具體如下:
關(guān)于surfaceview相關(guān)知識(shí):
view和surfaceview主要區(qū)別:
1. view只能在ui線程中刷新,而surfaceview可以在子線程中刷新
2. surfaceview可以控制刷新頻率
surfaceview幾個(gè)重要的方法:
1. 繼承surfaceview 后調(diào)用getholder()方法可以獲取到msurfaceholder對(duì)象這個(gè)對(duì)于可以控制surfaceview的繪制
2. 實(shí)現(xiàn)這個(gè)surfaceholder.callback接口并且msurfaceholder.addcallback(this)添加回調(diào)可以感知到surfaceview的生命周期
3. 繪制的時(shí)候mcanvas.drawcolor(color.black);這個(gè)方法很重要,這個(gè)方法是清理上一次繪制的東西,這個(gè)方法一定要調(diào)用才能看到效果
實(shí)現(xiàn)效果 如下:
第一步:新建xrsurfaceview繼承surfaceview
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.rong.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.surfaceholder; import android.view.surfaceview; /** * 自定義surfaceview * * @author 徐榮 * */ public class xrsurfaceview extends surfaceview implements surfaceholder.callback, runnable { // surfaceview的寬 int surfacewidth; // surfaceview的高 int surfaceheight; // surfaceholder對(duì)象 surfaceholder msurfaceholder; // 開關(guān)線程的標(biāo)志位 boolean isrunning = true ; // 畫筆 paint mpaint; // 圓的半徑 float radius = 0 ; // 圓是變大還是縮小的狀態(tài) boolean status = true ; // 圓變化的速度 int mspeed = 3 ; public xrsurfaceview(context context, attributeset attrs) { super (context, attrs); initview(); } private void initview() { // 獲取msurfaceholder msurfaceholder = getholder(); // 添加回調(diào) msurfaceholder.addcallback( this ); } @override public void surfacecreated(surfaceholder holder) { isrunning = true ; // 初始化畫筆 mpaint = new paint(); mpaint.setantialias( true ); mpaint.setcolor(color.blue); // 開啟繪制線程 new thread( this ).start(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // 獲取surface的寬 surfacewidth = width; // 獲取surface的高 surfaceheight = height; } @override public void surfacedestroyed(surfaceholder holder) { // 關(guān)閉繪制線程 isrunning = false ; } @override public void run() { canvas mcanvas = null ; while (isrunning) { try { // 鎖定canva進(jìn)行繪制 mcanvas = msurfaceholder.lockcanvas( null ); // 這個(gè)方法很重要,相當(dāng)于重繪(一定要調(diào)用不然看不到效果) mcanvas.drawcolor(color.black); // 畫圓 mcanvas.drawcircle((surfacewidth / 2 ), (surfaceheight / 2 ), radius, mpaint); // 更改半徑變量 if (status) { radius = radius + mspeed; if (radius > 200 ) { status = false ; } } else { radius = radius - mspeed; if (radius < 0 ) { status = true ; } } } catch (exception e) { e.printstacktrace(); } finally { // 解除畫布鎖 msurfaceholder.unlockcanvasandpost(mcanvas); } } } } |
第二步:新建布局文件activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#ffffff" android:orientation= "vertical" > <com.rong.activity.xrsurfaceview android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_centerinparent= "true" android:orientation= "vertical" /> </relativelayout> |
運(yùn)行!
希望本文所述對(duì)大家android程序設(shè)計(jì)有所幫助。