Android学习之surfaceview(一)

今天在学习Android游戏开发中的crazy football中遇到了一些问题。首先碰到的就是surfaceview相关的知识,由于之前没有接触过图像渲染方面的东西,觉得理解起来不太容易。因此专门将这个知识点拿出来学习一下。

       学习surfaceview之前,肯定有必要先了解一下view的基本知识。其实简单理解view就是每个activity界面的显示效果。系统自带了很多种效果,例如各种各样的布局文件,就是其中的很多种效果。我们也可以自己定义自己的效果。最简单的就是我们新建一个画布,在画布上绘制我们自己想要看的东西。这也是一种显示效果。不过自己定义的view有一个致命的缺点,它的参数更改只能在UI线程中进行。若要执行的任务比较消耗时间,就会导致UI线程长期的等待,从而程序死掉。下面是一个简单的自定义view代码:

 

 
[java]   view plain copy
  1. package com.example.test;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11.   
  12.      public class AnimateViewActivity extends Activity {     
  13.            
  14.            
  15.          protected void onCreate(Bundle savedInstanceState) {     
  16.              super.onCreate(savedInstanceState);     
  17.           
  18.           setContentView(new AnimateView(this));//這邊傳入的this代表這個對象,  
  19.             // setContentView(new DemoSurfaceView(this));//這邊傳入的this代表這個對象,  
  20. //  因 為Activity是繼承自Content類的,因此該對象也      
  21. //                                                  可向上轉型為Content類型作為   
  22.   
  23. //  AnimateView的構造方法的參數    
  24.           }    
  25.          
  26.          class AnimateView extends View{     
  27.            
  28.              float radius = 10;             
  29.              Canvas canvas=new Canvas();  
  30.              Paint paint=new Paint(); ;     
  31.                
  32.            
  33.              public AnimateView(Context context) {    
  34.                  super(context);    
  35.                      
  36.                 paint.setColor(Color.RED);     
  37.                  paint.setStyle(Paint.Style.FILL_AND_STROKE);    
  38.                  //onDraw(canvas);  
  39.             }    
  40.              
  41.              @Override      
  42.              protected void onDraw(Canvas canvas) {    
  43.             
  44.                  canvas.translate(200200);     
  45.                  canvas.drawCircle(8080, radius++, paint);              
  46.          
  47.                     if(radius > 100){    
  48.                       radius = 10;    
  49.                   }   
  50.                     
  51.                    invalidate();//通过调用这个方法让系统自动刷新视图          
  52.              }    
  53.           
  54.          }    
  55.           
  56.       }  
这段 代码实现了不断更新圆圈半径的显示。改变的过程是通过ondraw方法中的invalidate()方法实现的。
 
下面来看看surfaceview的使用方法:
      讲到surfaceVIEW,就得先提到与其相关联的其他几个名词:1,surface,2,surfaceholder,3 callback()
首先 来解释一下,surface其实就是一块内存区域,代表了一块显存。surfaceholder可以管理控制surfaceview,比如说控制画布canvas等
callback()函数管理了surface生命周期的三个主要函数,oncreat,onchange,ondestroy.
     使用的基本方法:
 1,创建类继承surfaceview 并实现callback接口
 2,初始化init,包括得到surfaceholder,(getholder()方法得到),利用holder来添加回调函数callback()
 3,   新建一个线程,来处理生命周期中所需要处理的动作(可选)
4, 在生命周期函数中,不断结束线程和开始线程,具体由动作决定(停止线程join()).
下面是一段实例代码:(只需要在上一段代码中添加这一句,并注释上一句就行// setContentView(new DemoSurfaceView(this));//這邊傳入的this代表這個對象,)
 
[java]   view plain copy
  1.     package com.example.test;    
  2.          
  3.     import android.content.Context;    
  4.     import android.graphics.Canvas;    
  5.     import android.graphics.Color;    
  6.     import android.graphics.Paint;    
  7.     import android.view.SurfaceHolder;    
  8.     import android.view.SurfaceHolder.Callback;    
  9.     import android.view.SurfaceView;    
  10.          
  11.     public class DemoSurfaceView extends SurfaceView  implements Callback{    
  12.          
  13.         LoopThread thread;    
  14.          
  15.         public DemoSurfaceView(Context context) {    
  16.             super(context);    
  17.          
  18.             init(); //初始化,设置生命周期回调方法    
  19.          
  20.         }    
  21.          
  22.         private void init(){    
  23.          
  24.             SurfaceHolder holder = getHolder();    
  25.             holder.addCallback(this); //设置Surface生命周期回调    
  26.             thread = new LoopThread(holder, getContext());    
  27.         }    
  28.          
  29.         @Override    
  30.         public void surfaceChanged(SurfaceHolder holder, int format, int width,    
  31.                 int height) {    
  32.         }    
  33.          
  34.         @Override    
  35.         public void surfaceCreated(SurfaceHolder holder) {    
  36.             thread.isRunning = true;    
  37.             thread.start();    
  38.         }    
  39.          
  40.         @Override    
  41.         public void surfaceDestroyed(SurfaceHolder holder) {    
  42.             thread.isRunning = false;    
  43.             try {    
  44.                 thread.join();    
  45.             } catch (InterruptedException e) {    
  46.                 e.printStackTrace();    
  47.             }    
  48.         }    
  49.          
  50.         /**  
  51.          * 执行绘制的绘制线程  
  52.          * @author Administrator  
  53.          *  
  54.          */    
  55.         class LoopThread extends Thread{    
  56.          
  57.             SurfaceHolder surfaceHolder;    
  58.             Context context;    
  59.             boolean isRunning;    
  60.             float radius = 10f;    
  61.             Paint paint;    
  62.          
  63.             public LoopThread(SurfaceHolder surfaceHolder,Context context){    
  64.          
  65.                 this.surfaceHolder = surfaceHolder;    
  66.                 this.context = context;    
  67.                 isRunning = false;    
  68.          
  69.                 paint = new Paint();    
  70.                 paint.setColor(Color.YELLOW);   
  71.                 paint.setTextSize(100);  
  72.                 paint.setStyle(Paint.Style.STROKE);    
  73.             }    
  74.          
  75.             @Override    
  76.             public void run() {    
  77.          
  78.                 Canvas c = null;    
  79.          
  80.                 while(isRunning){    
  81.          
  82.                     try{    
  83.                         synchronized (surfaceHolder) {    
  84.          
  85.                             c = surfaceHolder.lockCanvas(null);    
  86.                             doDraw(c);    
  87.                             //通过它来控制帧数执行一次绘制后休息50ms    
  88.                             Thread.sleep(50);    
  89.                         }    
  90.                     } catch (InterruptedException e) {    
  91.                         e.printStackTrace();    
  92.                     } finally {    
  93.                         surfaceHolder.unlockCanvasAndPost(c);    
  94.                     }    
  95.          
  96.                 }    
  97.          
  98.             }    
  99.          
  100.             public void doDraw(Canvas c){    
  101.          
  102.                 //这个很重要,清屏操作,清楚掉上次绘制的残留图像    
  103.                 c.drawColor(Color.BLACK);    
  104.          
  105.                 c.translate(200200);    
  106.                 c.rotate(30);  
  107. //              c.drawCircle(200,200, radius++, paint);    
  108.                 c.drawText("hello"+radius++, 5050, paint);  
  109.          
  110.                 if(radius > 100){    
  111.                     radius = 10f;    
  112.                 }    
  113.          
  114.             }    
  115.          
  116.         }    
  117.          
  118.     }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值