游戏框架SurfaceView的简单运用

SurfaceView运用

     android.view.SurfaceView是游戏开发中常用的框架,他继承android.view.View,与View相比,surfaceView是在一个新起的单独线程中可以重新绘制画面而View必须在UI的主线程中更新画面。因此开发游戏而言,如果属于要求主动更新界面的就用SurfaceView(例如赛车类,需要一个独立线程去更新界面,防止主线程阻塞),属于被动更新(棋类游戏)可以用View框架(调用invalidate方法更新)。

    上面粗糙的介绍了SurfaceView和View。下面来个简单的例子说明SurfaceView。

     创建一个MySurfaceView.java

public class MySurfaceView extends SurfaceView implements Callback, Runnable { // 打开程序后,创建一个矩形,并让矩形随着时间而移动

    int x = 0, y = 0, w = 40, h = 40, SrceenW, SrceenH; // 定义矩形的x,y坐标,宽高,以及手机的屏幕宽高
    SurfaceHolder surfaceHolder;
    Thread thread; // 定义一个线程,用来更新矩形所在的位置
    int sleepTime = 100;// 让线程100毫秒更新一次界面
    Paint paint; // 画笔
    boolean flag = false;// 线程是否启动

    public MySurfaceView(Context context, int SrceenW, int SrceenH) {
        super(context);
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        this.SrceenH = SrceenH;
        this.SrceenW = SrceenW;
        paint = new Paint();
        thread = new Thread(this);
    }

    /**
     * 
     */
    public void draw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        paint.setColor(Color.RED);
        canvas.drawRect(x, y, x + w, y + h, paint);
    }
    
    /**
     * 逻辑
     */
    public void logic() {
        if(x<=0){
            x=0;
        }
        if(y<=0){
            y=0;
        }
        if(x>=SrceenW-w){
            x=SrceenW-w;
        }
        if(y>=SrceenH-h){
            y=SrceenH-h;
        }
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder) { // 启动线程绘制
        // TODO Auto-generated method stub
        flag = true;
        if (!thread.isAlive()) {
            thread.start();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) { // 结束线程绘制
        // TODO Auto-generated method stub
        flag = false;
        thread = null;
    }

    @Override
    public void run() { // 绘制的更新过程
        // TODO Auto-generated method stub
        Canvas canvas = null;
        while (flag) {
            try {
                canvas = surfaceHolder.lockCanvas(); //锁定画布开始绘制
                synchronized (canvas) {
                    logic();
                    draw(canvas);
                }
            } catch (Exception e) {
                Log.e("mysurfaceview",e.getMessage());
            }finally{
                if(canvas!=null){
                    surfaceHolder.unlockCanvasAndPost(canvas);//解锁画布结束绘制
                }
            }
            x++;  //x,y坐标改变
            y++;
            try {
                Thread.sleep(sleepTime); //阻塞线程
            } catch (Exception e) {
                Log.e("mysurfaceview",e.getMessage());
            }
        }
    }

Activity中调用

public class SurfaceActivty extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 获取屏幕宽高
        Display display = getWindowManager().getDefaultDisplay();
        setContentView(new MySurfaceView(this,display.getWidth(),display.getHeight()));
    }
}

 

posted @ 2013-04-01 23:12  消逝的落叶  阅读(742)  评论(0编辑  收藏  举报