Android---SurfaceView(使游戏主角动起来)

新建两个类,一个继承Activity,一个继承SurfaceView并实现SurfaceHolder.Callback和Runnable接口,

将要实现动画效果的图片放到drawable中,我这里图片命名是以fish 开头的

 

SurfaceView 实现绘图的过程:

1.getHolder():得到这个SurfaceView的SurfaceHolder对象holder

2.holder.addCallback(callback):将holder添加到callback回调函数中,也就是实现SurfaceHolder.Callback接口的类对象

3.holder.lockCanvas():调用holder.lockCanvas()对象就得到SurfaceView对象对应的Canvas对象canvas,即取得画布

4.使用canvas进得画图

5.holder.unlockCanvasAndPost():调用holder.unlockCanvasAndPost()将绘制的图像解锁并显示在屏幕上

 

 

继承Activity的类,代码如下:

 

package com.mrzhu.test1;

import android.app.Activity;
import android.os.Bundle;

public class Test1Activity extends Activity {  
 public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(new SurfaceViewTest(this));  
 }  
}  


 

 

继承SurfaceView的类,代码如下:

 

package com.mrzhu.test1;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SurfaceViewTest extends SurfaceView implements Callback,Runnable {

 private SurfaceHolder holder;  
 private Canvas canvas;
 //声明十个容量的位图数组
 private Bitmap bitmap[] = new Bitmap[10];
 //记录当前播放帧
 private int currentFrame;
 private int dx = 5;
 private int dy = 5;
 private int left = 0;
 private int top = 0;
 private Thread th;
 private boolean flag;

 
 /**
  * SurfaceView初始化函数
  */
 public SurfaceViewTest(Context context) {
  super(context);
  holder = this.getHolder();//获取holder  
  holder.addCallback(this); //将holder添加到callback回调函数中
  for(int i = 0; i < bitmap.length; i++){
   bitmap[i] = BitmapFactory.decodeResource(getResources(), R.drawable.fish0 + i);
  }
 }

 /**
  * 绘图
  */
 @Override
 public void draw(Canvas canvas) {
  super.draw(canvas);
  canvas.drawBitmap(bitmap[currentFrame], left, top, null);
 }

 /**
  * 逻辑
  */
 private void logic(){
  currentFrame++;
  if(currentFrame >= bitmap.length){
   currentFrame = 0;
  }
 }

 public void run() {
  while(flag){
   long startTime = System.currentTimeMillis(); 
   //在这里加上线程安全锁
   synchronized (holder) {
    //拿到当前画布 然后锁定 
    canvas = holder.lockCanvas();
    canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
    //开始绘图
    draw(canvas);
    //判断是否碰到屏幕边界
    dx = left < 0 || left > (getWidth() - bitmap[currentFrame].getWidth())? - dx : dx;
    dy = top < 0 || top > (getHeight() - bitmap[currentFrame].getHeight())? - dy : dy;
    //调用方法使currentFrame增加
    logic();
    left = left +dx;
    top = top + dy;
     //绘制结束后解锁显示在屏幕上 
    holder.unlockCanvasAndPost(canvas);
   }
   //计算出游戏一次更新的毫秒数
   long endTime = System.currentTimeMillis();   
   //确保每次更新时间为30帧
   int diffTime  = (int)(endTime - startTime);       
   while(diffTime <= 30) {  
    //线程等待
    diffTime = (int)(System.currentTimeMillis() - startTime);          
    Thread.yield();  
   }
  }
 }

 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {

 }

 /**
  * SurfaceView视图创建,响应此函数
  */
 public void surfaceCreated(SurfaceHolder holder) {
  flag = true;
  //实例线程
  th = new Thread(this);
  //启动线程
  th.start();
 }

 /**
  * SurfaceView视图消亡时,响应此函数
  */
 public void surfaceDestroyed(SurfaceHolder holder) {
  flag = false;
 }
}


 

关于SurfaceView更详细的解释以及SurfaceView和View的区别,

以及线程方面的问题,大家请参考

http://xys289187120.blog.51cto.com/3361352/657181

 

资源下载:http://download.csdn.net/detail/zlqqhs/4684896
 

你可能感兴趣的:(Android---SurfaceView(使游戏主角动起来))