码迷,mamicode.com
首页 > 移动开发 > 详细

Android游戏开发(二):SurfaceView的使用

时间:2015-04-17 23:39:59      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

@author : qingdujun 

SurfaceView的使用挺简单的,只要实现继承SurfaceView并实现Callback接口即可;

package com.qdj.surfaceviewone;

import com.qdj.ui.SurfaceViewOne;

import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.view.WindowManager;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*set window*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /*define view by yourself*/
        setContentView(new SurfaceViewOne(this));
    }
}

自定义SurfaceView

package com.qdj.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SurfaceViewOne extends SurfaceView implements Callback, Runnable {

    private SurfaceHolder sfHolder;
    private Thread thread;
    private Canvas canvas;
    private Paint paint;
    int screenW, screenH;
    int clock;

    public SurfaceViewOne(Context context) {
        super(context);

        clock = 1;
        thread = new Thread(this);
        sfHolder = getHolder();
        sfHolder.addCallback(this);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        setKeepScreenOn(true);
    }

    private void draw() {
        try {
            /* get a canvas object. */
            canvas = sfHolder.lockCanvas();
            /* set bg color. */
            canvas.drawColor(Color.WHITE);
            canvas.drawText("this is a game." + (clock++), 100, 100, paint);
        } catch (Exception e) {

        } finally {
            /* Finish editing pixels in the surface. */
            if (canvas != null) {
                sfHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    @Override
    public void run() {

        while (true) {
            draw();

            try {
                /* delay 100ms to let us can see. */
                Thread.sleep(100);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        /* get screen size. */
        screenW = getWidth();
        screenH = getHeight();
        /* start the thread. */
        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

    }

}

 

Android游戏开发(二):SurfaceView的使用

标签:

原文地址:http://www.cnblogs.com/qingdujun/p/4436315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!