Android 的 SurfaceView 双缓冲应用

来源:互联网 发布:淘宝发的红包怎么领取 编辑:程序博客网 时间:2024/05/16 01:18

原文:http://www.oschina.net/code/snippet_54100_1422

 

     双缓冲是为了防止动画闪烁而实现的一种多线程应用,基于SurfaceView的双缓冲实现很简单,开一条线程并在其中绘图即可。本文介绍基于SurfaceView的双缓冲实现,以及介绍类似的更高效的实现方法。 

     本文程序运行截图如下,左边是开单个线程读取并绘图,右边是开两个线程,一个专门读取图片,一个专门绘图:

      对比一下,右边动画的帧速明显比左边的快,左右两者都没使用Thread.sleep()。为什么要开两个线程一个读一个画,而不去开两个线程像左边那样都 “边读边画”呢?因为SurfaceView每次绘图都会锁定Canvas,也就是说同一片区域这次没画完下次就不能画,因此要提高双缓冲的效率,就得开一条线程专门画图,开另外一条线程做预处理的工作。

 

     

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     android:orientation="vertical">  
  5.     <LinearLayout android:id="@+id/LinearLayout01"  
  6.         android:layout_width="wrap_content" android:layout_height="wrap_content">  
  7.         <Button android:id="@+id/Button01" android:layout_width="wrap_content"  
  8.             android:layout_height="wrap_content" android:text="单个独立线程"></Button>  
  9.         <Button android:id="@+id/Button02" android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content" android:text="两个独立线程"></Button>  
  11.     </LinearLayout>  
  12.     <SurfaceView android:id="@+id/SurfaceView01"  
  13.         android:layout_width="fill_parent" android:layout_height="fill_parent"></SurfaceView>  
  14. </LinearLayout>  
 

[java] view plaincopyprint?
  1. package com.testSurfaceView;  
  2. import java.lang.reflect.Field;  
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Rect;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.SurfaceHolder;  
  13. import android.view.SurfaceView;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. public class TestSurfaceView extends Activity {  
  17.     /** Called when the activity is first created. */  
  18.     Button btnSingleThread, btnDoubleThread;  
  19.     SurfaceView sfv;  
  20.     SurfaceHolder sfh;  
  21.     ArrayList<Integer> imgList = new ArrayList<Integer>();  
  22.     int imgWidth, imgHeight;  
  23.     Bitmap bitmap;//独立线程读取,独立线程绘图  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         btnSingleThread = (Button) this.findViewById(R.id.Button01);  
  29.         btnDoubleThread = (Button) this.findViewById(R.id.Button02);  
  30.         btnSingleThread.setOnClickListener(new ClickEvent());  
  31.         btnDoubleThread.setOnClickListener(new ClickEvent());  
  32.         sfv = (SurfaceView) this.findViewById(R.id.SurfaceView01);  
  33.         sfh = sfv.getHolder();  
  34.         sfh.addCallback(new MyCallBack());// 自动运行surfaceCreated以及surfaceChanged  
  35.     }  
  36.     class ClickEvent implements View.OnClickListener {  
  37.         @Override  
  38.         public void onClick(View v) {  
  39.             if (v == btnSingleThread) {  
  40.                 new Load_DrawImage(00).start();//开一条线程读取并绘图  
  41.             } else if (v == btnDoubleThread) {  
  42.                 new LoadImage().start();//开一条线程读取  
  43.                 new DrawImage(imgWidth + 100).start();//开一条线程绘图  
  44.             }  
  45.         }  
  46.     }  
  47.     class MyCallBack implements SurfaceHolder.Callback {  
  48.         @Override  
  49.         public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  50.                 int height) {  
  51.             Log.i("Surface:""Change");  
  52.         }  
  53.         @Override  
  54.         public void surfaceCreated(SurfaceHolder holder) {  
  55.             Log.i("Surface:""Create");  
  56.             // 用反射机制来获取资源中的图片ID和尺寸  
  57.             Field[] fields = R.drawable.class.getDeclaredFields();  
  58.             for (Field field : fields) {  
  59.                 if (!"icon".equals(field.getName()))// 除了icon之外的图片  
  60.                 {  
  61.                     int index = 0;  
  62.                     try {  
  63.                         index = field.getInt(R.drawable.class);  
  64.                     } catch (IllegalArgumentException e) {  
  65.                         // TODO Auto-generated catch block  
  66.                         e.printStackTrace();  
  67.                     } catch (IllegalAccessException e) {  
  68.                         // TODO Auto-generated catch block  
  69.                         e.printStackTrace();  
  70.                     }  
  71.                     // 保存图片ID  
  72.                     imgList.add(index);  
  73.                 }  
  74.             }  
  75.             // 取得图像大小  
  76.             Bitmap bmImg = BitmapFactory.decodeResource(getResources(),  
  77.                     imgList.get(0));  
  78.             imgWidth = bmImg.getWidth();  
  79.             imgHeight = bmImg.getHeight();  
  80.         }  
  81.         @Override  
  82.         public void surfaceDestroyed(SurfaceHolder holder) {  
  83.             Log.i("Surface:""Destroy");  
  84.         }  
  85.     }  
  86.     /** 
  87.      * 读取并显示图片的线程 
  88.      */  
  89.     class Load_DrawImage extends Thread {  
  90.         int x, y;  
  91.         int imgIndex = 0;  
  92.         public Load_DrawImage(int x, int y) {  
  93.             this.x = x;  
  94.             this.y = y;  
  95.         }  
  96.         public void run() {  
  97.             while (true) {  
  98.                 Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x  
  99.                         + imgWidth, this.y + imgHeight));  
  100.                 Bitmap bmImg = BitmapFactory.decodeResource(getResources(),  
  101.                         imgList.get(imgIndex));  
  102.                 c.drawBitmap(bmImg, this.x, this.y, new Paint());  
  103.                 imgIndex++;  
  104.                 if (imgIndex == imgList.size())  
  105.                     imgIndex = 0;  
  106.                 sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容  
  107.             }  
  108.         }  
  109.     };  
  110.     /** 
  111.      * 只负责绘图的线程 
  112.      */  
  113.     class DrawImage extends Thread {  
  114.         int x, y;  
  115.         public DrawImage(int x, int y) {  
  116.             this.x = x;  
  117.             this.y = y;  
  118.         }  
  119.         public void run() {  
  120.             while (true) {  
  121.                 if (bitmap != null) {//如果图像有效  
  122.                     Canvas c = sfh.lockCanvas(new Rect(this.x, this.y, this.x  
  123.                             + imgWidth, this.y + imgHeight));  
  124.                     c.drawBitmap(bitmap, this.x, this.y, new Paint());  
  125.                     sfh.unlockCanvasAndPost(c);// 更新屏幕显示内容  
  126.                 }  
  127.             }  
  128.         }  
  129.     };  
  130.     /** 
  131.      * 只负责读取图片的线程 
  132.      */  
  133.     class LoadImage extends Thread {  
  134.         int imgIndex = 0;  
  135.         public void run() {  
  136.             while (true) {  
  137.                 bitmap = BitmapFactory.decodeResource(getResources(),  
  138.                         imgList.get(imgIndex));  
  139.                 imgIndex++;  
  140.                 if (imgIndex == imgList.size())//如果到尽头则重新读取  
  141.                     imgIndex = 0;  
  142.             }  
  143.         }  
  144.     };  
  145. }  
 

原创粉丝点击