<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Android 的 SurfaceView 雙緩沖應用

Android 的 SurfaceView 雙緩沖應用

作者: 時(shí)間:2016-10-08 來(lái)源:網(wǎng)絡(luò ) 收藏

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

本文引用地址:http://dyxdggzs.com/article/201610/305442.htm

本文程序運行截圖如下,左邊是開(kāi)單個(gè)線(xiàn)程讀取并繪圖,右邊是開(kāi)兩個(gè)線(xiàn)程,一個(gè)專(zhuān)門(mén)讀取圖片,一個(gè)專(zhuān)門(mén)繪圖:

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

[圖片] 程序運行截圖

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=單個(gè)獨立線(xiàn)程>

10

11 android:layout_height=wrap_content android:text=兩個(gè)獨立線(xiàn)程>

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;//獨立線(xiàn)程讀取,獨立線(xiàn)程繪圖

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());// 自動(dòng)運行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();//開(kāi)一條線(xiàn)程讀取并繪圖

048 } else if (v == btnDoubleThread) {

049 new LoadImage().start();//開(kāi)一條線(xiàn)程讀取

050 new DrawImage(imgWidth + 10, 0).start();//開(kāi)一條線(xiàn)程繪圖

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 // 用反射機制來(lái)獲取資源中的圖片ID和尺寸

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

072 for (Field field : fields) {

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

074 {


上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>