當前位置:首頁 > 嵌入式 > 嵌入式軟件
[導讀] 雙緩沖是為了防止動畫閃爍而實現(xiàn)的一種多線程應用,基于SurfaceView的雙緩沖實現(xiàn)很簡單,開一條線程并在其中繪圖即可。本文介紹基于SurfaceView的雙緩沖實現(xiàn),以及介紹類似

雙緩沖是為了防止動畫閃爍而實現(xiàn)的一種多線程應用,基于SurfaceView的雙緩沖實現(xiàn)很簡單,開一條線程并在其中繪圖即可。本文介紹基于SurfaceView的雙緩沖實現(xiàn),以及介紹類似的更高效的實現(xiàn)方法。

本文程序運行截圖如下,左邊是開單個線程讀取并繪圖,右邊是開兩個線程,一個專門讀取圖片,一個專門繪圖:

對比一下,右邊動畫的幀速明顯比左邊的快,左右兩者都沒使用Thread.sleep()。為什么要開兩個線程一個讀一個畫,而不去開兩個線程像左邊那樣都 “邊讀邊畫”呢?因為SurfaceView每次繪圖都會鎖定Canvas,也就是說同一片區(qū)域這次沒畫完下次就不能畫,因此要提高雙緩沖的效率,就得開一條線程專門畫圖,開另外一條線程做預處理的工作。

[圖片] 程序運行截圖

2. [代碼]main.xml

01

02

03 android:layout_width="fill_parent" android:layout_height="fill_parent"

04 android:orientation="vertical">

05

06

07 android:layout_width="wrap_content" android:layout_height="wrap_content">

08

09 android:layout_height="wrap_content" android:text="單個獨立線程">

10

11 android:layout_height="wrap_content" android:text="兩個獨立線程">

12

13

14 android:layout_width="fill_parent" android:layout_height="fill_parent">

15

3. [代碼]TestSurfaceView.java

001package com.testSurfaceView;

002

003import java.lang.reflect.Field;

004import java.util.ArrayList;

005import android.app.Activity;

006import android.graphics.Bitmap;

007import android.graphics.BitmapFactory;

008import android.graphics.Canvas;

009import android.graphics.Paint;

010import android.graphics.Rect;

011import android.os.Bundle;

012import android.util.Log;

013import android.view.SurfaceHolder;

014import android.view.SurfaceView;

015import android.view.View;

016import android.widget.Button;

017

018public class TestSurfaceView extends Activity {

019 /** Called when the activity is first created. */

020 Button btnSingleThread, btnDoubleThread;

021 SurfaceView sfv;

022 SurfaceHolder sfh;

023 ArrayList imgList = new ArrayList();

024 int imgWidth, imgHeight;

025 Bitmap bitmap;//獨立線程讀取,獨立線程繪圖

026

027 @Override

028 public void onCreate(Bundle savedInstanceState) {

029 super.onCreate(savedInstanceState);

030 setContentView(R.layout.main);

031

032 btnSingleThread = (Button) this.findViewById(R.id.Button01);

033 btnDoubleThread = (Button) this.findViewById(R.id.Button02);

034 btnSingleThread.setOnClickListener(new ClickEvent());

035 btnDoubleThread.setOnClickListener(new ClickEvent());

036 sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);

037 sfh = sfv.getHolder();

038 sfh.addCallback(new MyCallBack());// 自動運行surfaceCreated以及surfaceChanged

039 }

040

041 class ClickEvent implements View.OnClickListener {

042

043 @Override

044 public void onClick(View v) {

045

046 if (v == btnSingleThread) {

047 new Load_DrawImage(0, 0).start();//開一條線程讀取并繪圖

048 } else if (v == btnDoubleThread) {

049 new LoadImage().start();//開一條線程讀取

050 new DrawImage(imgWidth + 10, 0).start();//開一條線程繪圖

051 }

052

053 }

054

055 }

056

057 class MyCallBack implements SurfaceHolder.Callback {

058

059 @Override

060 public void surfaceChanged(SurfaceHolder holder, int format, int width,

061 int height) {

062 Log.i("Surface:", "Change");

063

064 }

065

066 @Override

067 public void surfaceCreated(SurfaceHolder holder) {

068 Log.i("Surface:", "Create");

069

070 // 用反射機制來獲取資源中的圖片ID和尺寸

071 Field[] fields = R.drawable.class.getDeclaredFields();

072 for (Field field : fields) {

073 if (!"icon".equals(field.getName()))// 除了icon之外的圖片

074 {

[!--empirenews.page--]

075 int index = 0;

076 try {

077 index = field.getInt(R.drawable.class);

078 } catch (IllegalArgumentException e) {

079 // TODO Auto-generated catch block

080 e.printStackTrace();

081 } catch (IllegalAccessException e) {

082 // TODO Auto-generated catch block

083 e.printStackTrace();

084 }

085 // 保存圖片ID

086 imgList.add(index);

087 }

088 }

089 // 取得圖像大小

090 Bitmap bmImg = BitmapFactory.decodeResource(getResources(),

091 imgList.get(0));

092 imgWidth = bmImg.getWidth();

093 imgHeight = bmImg.getHeight();

094 }

095

096 @Override

097 public void surfaceDestroyed(SurfaceHolder holder) {

098 Log.i("Surface:", "Destroy");

099

100 }

101

102 }

103

104 /**

105 * 讀取并顯示圖片的線程

106 */

107 class Load_DrawImage extends Thread {

108 int x, y;

109 int imgIndex = 0;

110

111 public Load_DrawImage(int x, int y) {

112 this.x = x;

113 this.y = y;

114 }

115

116 public void run() {

117 while (true) {

118 Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x

119 + imgWidth, this.y + imgHeight));

120 Bitmap bmImg = BitmapFactory.decodeResource(getResources(),

121 imgList.get(imgIndex));

122 c.drawBitmap(bmImg, this.x, this.y, new Paint());

123 imgIndex++;

124 if (imgIndex == imgList.size())

125 imgIndex = 0;

126

127 sfh.unlockCanvasAndPost(c);// 更新屏幕顯示內容

128 }

129 }

130 };

131

132 /**

133 * 只負責繪圖的線程

134 */

135 class DrawImage extends Thread {

136 int x, y;

137

138 public DrawImage(int x, int y) {

139 this.x = x;

140 this.y = y;

141 }

142

143 public void run() {

144 while (true) {

145 if (bitmap != null) {//如果圖像有效

146 Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x

147 + imgWidth, this.y + imgHeight));

148

149 c.drawBitmap(bitmap, this.x, this.y, new Paint());

150

151 sfh.unlockCanvasAndPost(c);// 更新屏幕顯示內容

152 }

153 }

154 }

155 };

156

157 /**

158 * 只負責讀取圖片的線程

159 */

160 class LoadImage extends Thread {

161 int imgIndex = 0;

162

163 public void run() {

164 while (true) {

165 bitmap = BitmapFactory.decodeResource(getResources(),

166 imgList.get(imgIndex));

167 imgIndex++;

168 if (imgIndex == imgList.size())//如果到盡頭則重新讀取

169 imgIndex = 0;

170 }

171 }

172 };

 

173}

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或將催生出更大的獨角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉型技術解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關鍵字: 汽車 人工智能 智能驅動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質量流程IT總裁陶景文發(fā)表了演講。

關鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權最終是由生態(tài)的繁榮決定的。

關鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質量發(fā)展策略,塑強核心競爭優(yōu)勢...

關鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術學會聯(lián)合牽頭組建的NVI技術創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術創(chuàng)新聯(lián)...

關鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關鍵字: BSP 信息技術
關閉
關閉