Android 的 SurfaceView 雙緩沖應用
雙緩沖是為了防止動畫閃爍而實現(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
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);// 更新屏幕顯示內(nèi)容
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);// 更新屏幕顯示內(nèi)容
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}