




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
精品文檔-下載后可編輯SurfaceView的雙緩沖使用AndroidAndroid一詞的本義指“機器人”,同時也是Google于2022年11月5日宣布的基于Linux平臺的開源手機操作系統(tǒng)的名稱,該平臺由操作系統(tǒng)、中間件、用戶界面和應用軟件組成,號稱是為移動終端打造的真正開放和完整的移動軟件。目前,版本為Android2.4Gingerbread和Android3.0Honeycomb。
Android是基于Linux開放性內(nèi)核的操作系統(tǒng),是Google公司在2022年11月5日公布的手機操作系統(tǒng)。早期由原名為"Android"的公司開發(fā),谷歌在2022年收購"Android.Inc"后,繼續(xù)進行對Android系統(tǒng)開發(fā)運營,它采用了軟件堆層的架構,主要分為三部分。底層Linux內(nèi)核只提供基本功能,其他的應用軟件則由各公司自行開發(fā),部分程序以Java編寫。
這次介紹SurfaceView的雙緩沖使用。雙緩沖是為了防止動畫閃爍而實現(xiàn)的一種多線程應用,基于SurfaceView的雙緩沖實現(xiàn)很簡單,開一條線程并在其中繪圖即可。本文介紹基于SurfaceView的雙緩沖實現(xiàn),以及介紹類似的更高效的實現(xiàn)方法。
本文程序運行截圖如下,左邊是開單個線程讀取并繪圖,右邊是開兩個線程,一個專門讀取圖片,一個專門繪圖:
對比一下,右邊動畫的幀速明顯比左邊的快,左右兩者都沒使用Thread.sleep()。為什么要開兩個線程一個讀一個畫,而不去開兩個線程像左邊那樣都“邊讀邊畫”呢?因為SurfaceView每次繪圖都會鎖定Canvas,也就是說同一片區(qū)域這次沒畫完下次就不能畫,因此要提高雙緩沖的效率,就得開一條線程專門畫圖,開另外一條線程做預處理的工作。
main.xml的源碼:
viewplaincopytoclipboardprint?
android:orientation="vertical"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_height="wrap_content"android:text="單個獨立線程"android:layout_height="wrap_content"android:text="兩個獨立線程"android:layout_width="fill_parent"android:layout_height="fill_parent"
android:layout_width="fill_parent"android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_height="wrap_content"android:text="單個獨立線程"android:layout_height="wrap_content"android:text="兩個獨立線程"
android:layout_width="fill_parent"android:layout_height="fill_parent"
本文程序的源碼:
viewplaincopytoclipboardprint?
packagecom.testSurfaceView;
importjava.lang.reflect.Field;
importjava.util.ArrayList;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceView;
importandroid.view.View;
importandroid.widget.Button;
publicclasstestSurfaceViewextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
ButtonbtnSingleThread,btnDoubleThread;
SurfaceViewsfv;
SurfaceHoldersfh;
ArrayListimgList=newArrayList();
intimgWidth,imgHeight;
Bitmapbitmap;//獨立線程讀取,獨立線程繪圖
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSingleThread=(Button)this.findViewById(R.id.Button01);
btnDoubleThread=(Button)this.findViewById(R.id.Button02);
btnSingleThread.setOnClickListener(newClickEvent());
btnDoubleThread.setOnClickListener(newClickEvent());
sfv=(SurfaceView)this.findViewById(R.id.SurfaceView01);
sfh=sfv.getHolder();
sfh.addCallback(newMyCallBack());//自動運行surfaceCreated以及surfaceChanged
}
classClickEventimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
if(v==btnSingleThread){
newLoad_DrawImage(0,0)。start();//開一條線程讀取并繪圖
}elseif(v==btnDoubleThread){
newLoadImage()。start();//開一條線程讀取
newDrawImage(imgWidth+10,0)。start();//開一條線程繪圖
}
}
}
classMyCallBackimplementsSurfaceHolder.Callback{
@Override
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
intheight){
Log.i("Surface:","Change");
}
@Override
publicvoidsurfaceCreated(SurfaceHolderholder){
Log.i("Surface:","Create");
//用反射機制來獲取資源中的圖片ID和尺寸
Field[]fields=R.drawable.class.getDeclaredFields();
for(Fieldfield:fields){
if(!"icon".equals(field.getName()))//除了icon之外的圖片
{
intindex=0;
try{
index=field.getInt(R.drawable.class);
}catch(IllegalArgumentExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//保存圖片ID
imgList.add(index);
}
}
//取得圖像大小
BitmapbmImg=BitmapFactory.decodeResource(getResources(),
imgList.get(0));
imgWidth=bmImg.getWidth();
imgHeight=bmImg.getHeight();
}
@Override
publicvoidsurfaceDestroyed(SurfaceHolderholder){
Log.i("Surface:","Destroy");
}
}
/*
*讀取并顯示圖片的線程
*/
classLoad_DrawImageextendsThread{
intx,y;
intimgIndex=0;
publicLoad_DrawImage(intx,inty){
this.x=x;
this.y=y;
}
publicvoidrun(){
while(true){
Canvasc=sfh.lockCanvas(newRect(this.x,this.y,this.x
+imgWidth,this.y+imgHeight));
BitmapbmImg=BitmapFactory.decodeResource(getResources(),
imgList.get(imgIndex));
c.drawBitmap(bmImg,this.x,this.y,newPaint());
imgIndex++;
if(imgIndex==imgList.size())
imgIndex=0;
sfh.unlockCanvasAndPost(c);//更新屏幕顯示內(nèi)容
}
}
};
/*
*只負責繪圖的線程
*/
classDrawImageextendsThread{
intx,y;
publicDrawImage(intx,inty){
this.x=x;
this.y=y;
}
publicvoidrun(){
while(true){
if(bitmap!=null){//
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國高壓噴霧車市場分析及競爭策略研究報告
- 2025至2030年中國透氣帽市場分析及競爭策略研究報告
- 2025至2030年中國聚氨酯發(fā)泡保溫巖棉制品市場分析及競爭策略研究報告
- 2025至2030年中國立式單層儲液罐市場分析及競爭策略研究報告
- 2025至2030年中國焊接銷套市場分析及競爭策略研究報告
- 2025至2030年中國橋梁組合鋼模板市場分析及競爭策略研究報告
- 2025至2030年中國方便車市場分析及競爭策略研究報告
- 2025至2030年中國微機遠動力學終端單元市場分析及競爭策略研究報告
- 2025至2030年中國參鹿強身膠囊市場分析及競爭策略研究報告
- 2025至2030年中國人棉粒粒布市場分析及競爭策略研究報告
- YY/T 1851-2022用于增材制造的醫(yī)用純鉭粉末
- GB/Z 13800-2021手動輪椅車
- GB/T 6109.17-2008漆包圓繞組線第17部分:180級自粘性直焊聚酯亞胺漆包銅圓線
- GB/T 5163-2006燒結金屬材料(不包括硬質合金)可滲性燒結金屬材料密度、含油率和開孔率的測定
- 英語學科核心素養(yǎng)教案設計
- 小學二年級數(shù)學下冊找規(guī)律復習題
- GPS與慣導系統(tǒng)的組合導航技術課件
- 2020-2021年度廣東省湛江市赤坎區(qū)教師縣鄉(xiāng)選調(diào)招聘考試《教育基礎知識》試卷及答案【解析】
- DB15T 489-2019 石油化學工業(yè)建設工程技術資料管理規(guī)范
- (新版)無人機駕駛員資格理論考試題庫及答案
- HALCON編程基礎與工程應用全書ppt課件匯總(完整版)
評論
0/150
提交評論