SurfaceView使用方法简介-来自网络

本文来自 http://blog.csdn.net/hellogv/ ,引用必须注明出处!

          SurfaceView由于可以直接从内存或者DMA等硬件接口取得图像数据,因此是个非常重要的绘图容器,这次 我就用两篇文章来介绍SurfaceView的用法。网上介绍SurfaceView的用法有很多,写法也层出不同,例如继承SurfaceView类, 或者继承SurfaceHolder.Callback类等,这个可以根据功能实际需要自己选择,我这里就直接在普通的用户界面调用 SurfaceHolder的lockCanvas和unlockCanvasAndPost。

        先来看看程序运行的截图:

截图1主要演示了直接把正弦波绘画在SurfaceView上

对比上面的左右两图,右图用.lockCanvas(null),而左图用.lockCanvas(new Rect(oldX, 0, oldX + length,
    getWindowManager().getDefaultDisplay().getHeight())), 对比一下两个效果,由于左图是按指定Rect绘画,所以效率会比右图的全控件绘画高些,并且在清屏之后 (canvas.drawColor(Color.BLACK))不会留有上次绘画的残留。

 

接下来贴出main.xml的源码:

  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.   
  6.     < LinearLayout   android:id = "@+id/LinearLayout01"   
  7.         android:layout_width = "wrap_content"   android:layout_height = "wrap_content" >   
  8.         < Button   android:id = "@+id/Button01"   android:layout_width = "wrap_content"   
  9.             android:layout_height = "wrap_content"   android:text = "简单绘画" > </ Button >   
  10.         < Button   android:id = "@+id/Button02"   android:layout_width = "wrap_content"   
  11.             android:layout_height = "wrap_content"   android:text = "定时器绘画" > </ Button >   
  12.     </ LinearLayout >   
  13.     < SurfaceView   android:id = "@+id/SurfaceView01"   
  14.         android:layout_width = "fill_parent"   android:layout_height = "fill_parent" > </ SurfaceView >   
  15. </ LinearLayout >   

 

接下来贴出程序源码:

  1. package  com.testSurfaceView;  
  2.   
  3. import  java.util.Timer;  
  4. import  java.util.TimerTask;  
  5.   
  6. import  android.app.Activity;  
  7. import  android.graphics.Canvas;  
  8. import  android.graphics.Color;  
  9. import  android.graphics.Paint;  
  10. import  android.graphics.Rect;  
  11. import  android.os.Bundle;  
  12. import  android.util.Log;  
  13. import  android.view.SurfaceHolder;  
  14. import  android.view.SurfaceView;  
  15. import  android.view.View;  
  16. import  android.widget.Button;  
  17.   
  18. public   class  testSurfaceView  extends  Activity {  
  19.     /** Called when the activity is first created. */   
  20.     Button btnSimpleDraw, btnTimerDraw;  
  21.     SurfaceView sfv;  
  22.     SurfaceHolder sfh;  
  23.   
  24.     private  Timer mTimer;  
  25.     private  MyTimerTask mTimerTask;  
  26.     int  Y_axis[], //保存正弦波的Y轴上的点   
  27.     centerY,//中心线   
  28.     oldX,oldY,//上一个XY点    
  29.     currentX;//当前绘制到的X轴上的点   
  30.   
  31.     @Override   
  32.     public   void  onCreate(Bundle savedInstanceState) {  
  33.         super .onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.   
  36.         btnSimpleDraw = (Button) this .findViewById(R.id.Button01);  
  37.         btnTimerDraw = (Button) this .findViewById(R.id.Button02);  
  38.         btnSimpleDraw.setOnClickListener(new  ClickEvent());  
  39.         btnTimerDraw.setOnClickListener(new  ClickEvent());  
  40.         sfv = (SurfaceView) this .findViewById(R.id.SurfaceView01);  
  41.         sfh = sfv.getHolder();  
  42.   
  43.         //动态绘制正弦波的定时器   
  44.         mTimer = new  Timer();  
  45.         mTimerTask = new  MyTimerTask();  
  46.   
  47.         // 初始化y轴数据   
  48.         centerY = (getWindowManager().getDefaultDisplay().getHeight() - sfv  
  49.                 .getTop()) / 2 ;  
  50.         Y_axis = new   int [getWindowManager().getDefaultDisplay().getWidth()];  
  51.         for  ( int  i =  1 ; i < Y_axis.length; i++) { // 计算正弦波   
  52.             Y_axis[i - 1 ] = centerY  
  53.                     - (int ) ( 100  * Math.sin(i *  2  * Math.PI /  180 ));  
  54.         }  
  55.     }  
  56.   
  57.     class  ClickEvent  implements  View.OnClickListener {  
  58.   
  59.         @Override   
  60.         public   void  onClick(View v) {  
  61.   
  62.             if  (v == btnSimpleDraw) {  
  63.                 SimpleDraw(Y_axis.length-1 ); //直接绘制正弦波   
  64.               
  65.             } else   if  (v == btnTimerDraw) {  
  66.                 oldY = centerY;  
  67.                 mTimer.schedule(mTimerTask, 05 ); //动态绘制正弦波   
  68.             }  
  69.   
  70.         }  
  71.   
  72.     }  
  73.   
  74.     class  MyTimerTask  extends  TimerTask {  
  75.         @Override   
  76.         public   void  run() {  
  77.   
  78.             SimpleDraw(currentX);  
  79.             currentX++;//往前进   
  80.             if  (currentX == Y_axis.length -  1 ) { //如果到了终点,则清屏重来   
  81.                 ClearDraw();  
  82.                 currentX = 0 ;  
  83.                 oldY = centerY;  
  84.             }  
  85.         }  
  86.   
  87.     }  
  88.       
  89.     /*  
  90.      * 绘制指定区域  
  91.      */   
  92.     void  SimpleDraw( int  length) {  
  93.         if  (length ==  0 )  
  94.             oldX = 0 ;  
  95.         Canvas canvas = sfh.lockCanvas(new  Rect(oldX,  0 , oldX + length,  
  96.                 getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布   
  97.         Log.i("Canvas:" ,  
  98.                 String.valueOf(oldX) + ","  + String.valueOf(oldX + length));  
  99.   
  100.         Paint mPaint = new  Paint();  
  101.         mPaint.setColor(Color.GREEN);// 画笔为绿色   
  102.         mPaint.setStrokeWidth(2 ); // 设置画笔粗细   
  103.   
  104.         int  y;  
  105.         for  ( int  i = oldX +  1 ; i < length; i++) { // 绘画正弦波   
  106.             y = Y_axis[i - 1 ];  
  107.             canvas.drawLine(oldX, oldY, i, y, mPaint);  
  108.             oldX = i;  
  109.             oldY = y;  
  110.         }  
  111.         sfh.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像   
  112.     }  
  113.   
  114.     void  ClearDraw() {  
  115.         Canvas canvas = sfh.lockCanvas(null );  
  116.         canvas.drawColor(Color.BLACK);// 清除画布   
  117.         sfh.unlockCanvasAndPost(canvas);  
  118.   
  119.     }  
  120. }  

 

注意一下 for (int i = oldX + 1; i < length; i++) {// 绘画正弦波 这句,在.lockCanvas()指定Rect内减少循环画线的次数,可以提高绘图效率。


上一篇 简 单介绍了SurfaceView的基本使用,这次就介绍SurfaceView与多线程的混搭。SurfaceView与多线程混搭,是为了防止动画闪烁 而实现的一种多线程应用。android的多线程用法与JAVA的多线程用法完全一样,本文不做多线程方面的介绍了。直接讲解SurfaceView与多 线程的混合使用,即开一条线程专门读取图片,另外一条线程专门绘图。

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

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

main.xml的源码:

  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.   
  6.     < LinearLayout   android:id = "@+id/LinearLayout01"   
  7.         android:layout_width = "wrap_content"   android:layout_height = "wrap_content" >   
  8.         < Button   android:id = "@+id/Button01"   android:layout_width = "wrap_content"   
  9.             android:layout_height = "wrap_content"   android:text = "单个独立线程" > </ Button >   
  10.         < Button   android:id = "@+id/Button02"   android:layout_width = "wrap_content"   
  11.             android:layout_height = "wrap_content"   android:text = "两个独立线程" > </ Button >   
  12.     </ LinearLayout >   
  13.     < SurfaceView   android:id = "@+id/SurfaceView01"   
  14.         android:layout_width = "fill_parent"   android:layout_height = "fill_parent" > </ SurfaceView >   
  15. </ LinearLayout >   

 

本文程序的源码:

 

  1. package  com.testSurfaceView;  
  2.   
  3. import  java.lang.reflect.Field;  
  4. import  java.util.ArrayList;  
  5. import  android.app.Activity;  
  6. import  android.graphics.Bitmap;  
  7. import  android.graphics.BitmapFactory;  
  8. import  android.graphics.Canvas;  
  9. import  android.graphics.Paint;  
  10. import  android.graphics.Rect;  
  11. import  android.os.Bundle;  
  12. import  android.util.Log;  
  13. import  android.view.SurfaceHolder;  
  14. import  android.view.SurfaceView;  
  15. import  android.view.View;  
  16. import  android.widget.Button;  
  17.   
  18. public   class  testSurfaceView  extends  Activity {  
  19.     /** Called when the activity is first created. */   
  20.     Button btnSingleThread, btnDoubleThread;  
  21.     SurfaceView sfv;  
  22.     SurfaceHolder sfh;  
  23.     ArrayList<Integer> imgList = new  ArrayList<Integer>();  
  24.     int  imgWidth, imgHeight;  
  25.     Bitmap bitmap;//独立线程读取,独立线程绘图   
  26.   
  27.     @Override   
  28.     public   void  onCreate(Bundle savedInstanceState) {  
  29.         super .onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.   
  32.         btnSingleThread = (Button) this .findViewById(R.id.Button01);  
  33.         btnDoubleThread = (Button) this .findViewById(R.id.Button02);  
  34.         btnSingleThread.setOnClickListener(new  ClickEvent());  
  35.         btnDoubleThread.setOnClickListener(new  ClickEvent());  
  36.         sfv = (SurfaceView) this .findViewById(R.id.SurfaceView01);  
  37.         sfh = sfv.getHolder();  
  38.         sfh.addCallback(new  MyCallBack()); // 自动运行surfaceCreated以及surfaceChanged   
  39.     }  
  40.   
  41.     class  ClickEvent  implements  View.OnClickListener {  
  42.   
  43.         @Override   
  44.         public   void  onClick(View v) {  
  45.   
  46.             if  (v == btnSingleThread) {  
  47.                 new  Load_DrawImage( 00 ).start(); //开一条线程读取并绘图   
  48.             } else   if  (v == btnDoubleThread) {  
  49.                 new  LoadImage().start(); //开一条线程读取   
  50.                 new  DrawImage(imgWidth +  100 ).start(); //开一条线程绘图   
  51.             }  
  52.   
  53.         }  
  54.   
  55.     }  
  56.   
  57.     class  MyCallBack  implements  SurfaceHolder.Callback {  
  58.   
  59.         @Override   
  60.         public   void  surfaceChanged(SurfaceHolder holder,  int  format,  int  width,  
  61.                 int  height) {  
  62.             Log.i("Surface:""Change" );  
  63.   
  64.         }  
  65.   
  66.         @Override   
  67.         public   void  surfaceCreated(SurfaceHolder holder) {  
  68.             Log.i("Surface:""Create" );  
  69.   
  70.             // 用反射机制来获取资源中的图片ID和尺寸   
  71.             Field[] fields = R.drawable.class .getDeclaredFields();  
  72.             for  (Field field : fields) {  
  73.                 if  (! "icon" .equals(field.getName())) // 除了icon之外的图片   
  74.                 {  
  75.                     int  index =  0 ;  
  76.                     try  {  
  77.                         index = field.getInt(R.drawable.class );  
  78.                     } catch  (IllegalArgumentException e) {  
  79.                         // TODO Auto-generated catch block   
  80.                         e.printStackTrace();  
  81.                     } catch  (IllegalAccessException e) {  
  82.                         // TODO Auto-generated catch block   
  83.                         e.printStackTrace();  
  84.                     }  
  85.                     // 保存图片ID   
  86.                     imgList.add(index);  
  87.                 }  
  88.             }  
  89.             // 取得图像大小   
  90.             Bitmap bmImg = BitmapFactory.decodeResource(getResources(),  
  91.                     imgList.get(0 ));  
  92.             imgWidth = bmImg.getWidth();  
  93.             imgHeight = bmImg.getHeight();  
  94.         }  
  95.   
  96.         @Override   
  97.         public   void  surfaceDestroyed(SurfaceHolder holder) {  
  98.             Log.i("Surface:""Destroy" );  
  99.   
  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.     };  

 

 

 

 

 

要创建一个新的SurfaceView,需要创建一个新的扩展了SurfaceView的类,并实现SurfaceHolder.Callback。

SurfaceHolder回调可以在底层的Surface被创建和销毁的时候通知View,并传递给它对SurfaceHolder对象的引用,其中包含了当前有效的Surface。

一个典型的Surface View设计模型包括一个由Thread所派生的类,它可以接收对当前的SurfaceHolder的引用,并独立地更新它。

下面的框架代码展示了使用Canvas所绘制的Surface View的实现。在Surface View控件中创建了一个新的由Thread派生的类,并且所有的UI更新都是在这个新类中处理的。

 
 
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.view.SurfaceHolder;  
  4. import android.view.SurfaceView;  
  5.  
  6. public class MySurfaceView extends SurfaceView implements SurfaceHolder. Callback {  
  7.  
  8.   private SurfaceHolder holder;  
  9.   private MySurfaceViewThread mySurfaceViewThread;  
  10.   private boolean hasSurface;  
  11.  
  12.   MySurfaceView(Context context) {  
  13.     super(context);  
  14.     init();  
  15.   }  
  16.    
  17.   private void init() {  
  18.     //创建一个新的SurfaceHolder, 并分配这个类作为它的回调(callback)  
  19.      holder  =  getHolder ();  
  20.     holder.addCallback(this);  
  21.      hasSurface  =  false ;  
  22.   }  
  23.  
  24.   public void resume() {  
  25.     //创建和启动图像更新线程  
  26.     if ( mySurfaceViewThread  == null) {  
  27.        mySurfaceViewThread  =  new  MySurfaceViewThread();  
  28.       if ( hasSurface  == true)  
  29.         mySurfaceViewThread.start();  
  30.     }  
  31.   }  
  32.  
  33.   public void pause() {  
  34.     // 杀死图像更新线程  
  35.     if (mySurfaceViewThread != null) {  
  36.       mySurfaceViewThread.requestExitAndWait();  
  37.        mySurfaceViewThread  =  null ;  
  38.     }  
  39.   }  
  40.  
  41.   public void surfaceCreated(SurfaceHolder holder) {  
  42.      hasSurface  =  true ;  
  43.     if (mySurfaceViewThread != null)  
  44.       mySurfaceViewThread.start();  
  45.   }  
  46.  
  47.   public void surfaceDestroyed(SurfaceHolder holder) {  
  48.      hasSurface  =  false ;  
  49.     pause();  
  50.   }  
  51.  
  52.   public void surfaceChanged(SurfaceHolder holder,int format,int w,int h) {  
  53.     if (mySurfaceViewThread != null)  
  54.       mySurfaceViewThread.onWindowResize(w, h);  
  55.   }  
  56.  
  57.   class MySurfaceViewThread extends Thread {  
  58.     private boolean done;  
  59.  
  60.     MySurfaceViewThread() {  
  61.       super();  
  62.        done  =  false ;  
  63.     }  
  64.  
  65.     @Override  
  66.     public void run() {  
  67.       SurfaceHolder  surfaceHolder  =  holder ;  
  68.  
  69.       // 重复绘图循环,直到线程停止  
  70.       while (!done) {  
  71.         // 锁定surface,并返回到要绘图的Canvas  
  72.         Canvas  canvas  =  surfaceHolder .lockCanvas();  
  73.  
  74.         // 待实现:在Canvas上绘图  
  75.  
  76.         // 解锁Canvas,并渲染当前图像  
  77.         surfaceHolder.unlockCanvasAndPost(canvas);  
  78.       }  
  79.     }  
  80.  
  81.     public void requestExitAndWait() {  
  82.       // 把这个线程标记为完成,并合并到主程序线程  
  83.        done  =  true ;  
  84.       try {  
  85.         join();  
  86.       } catch (InterruptedException ex) { }  
  87.     }  
  88.  
  89.     public void onWindowResize(int w, int h) {  
  90.       // 处理可用的屏幕尺寸的改变  
  91.     }  
  92.   }  
  93. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值