Android SurfaceView的基本用法

说明:
第一步:继承SurfaceView 并实现Callback,Runable
第二步:初始化SurfaceHolder mHolder,并添加事件
mHolder = getHolder();
mHolder.addCallback(this);
第三步:在public void surfaceCreated方法里开启线程, 在public void surfaceDestroyed关闭线程
第四步:在public void run()方法里实现draw方法
第五步:如果要添加背景,在draw()方法里实现,在onSizeChanged()方法里可得到改变后的视图宽高及矩形矩阵

具体代码如下:

public class FlyBirdView extends SurfaceView implements Callback, Runnable {
    private SurfaceHolder mHolder;
    // private Thread mThread;
    private ExecutorService mPool;
    private Canvas mCanvas;
    private boolean isRunnging;// 是否运行

    // 二.设置背景
    private Bitmap mBgBitmap;
    //当前View的尺寸
    private int mWidth;
    private int mHeight;
    private RectF mGamePanelRect = new RectF();

    // ----构造函数处理---------------------------------------------
    public FlyBirdView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlyBirdView(Context context) {
        this(context, null);
    }

    public FlyBirdView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // -初始化holer-----------------------
        mHolder = getHolder();
        mHolder.addCallback(this);
        setZOrderOnTop(true);
        // 设置画布 背景透明
        mHolder.setFormat(PixelFormat.TRANSLUCENT);

        // --焦点设置----------------------------
        setFocusable(true);
        // 设置触屏
        setFocusableInTouchMode(true);
        // 设置常亮
        setKeepScreenOn(true);

        // --背景设置--------------------------------
        mGamePanelRect = new RectF();
        mBgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgbird);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mWidth = w;
        mHeight = h;
        mGamePanelRect.set(0, 0, w, h);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void run() {
        while (isRunnging) {
            long start = System.currentTimeMillis();
            draw();
            long end = System.currentTimeMillis();
            if (start - end < 50) {
                SystemClock.sleep(50 - (start - end));
            }
        }
    }

    private void draw() {
        try {
            if (mHolder != null) {
                mCanvas = mHolder.lockCanvas();

                if (mCanvas != null) {
                    //绘制背景
                    drawBg();  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mHolder != null && mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);
            }

        }

    }

    private void drawBg() {
        mCanvas.drawBitmap(mBgBitmap,null, mGamePanelRect, null);
    }

    // ---callback监听------------------------------------------------------
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // -线程处理--------------------------
        isRunnging = true;
        mPool = Executors.newFixedThreadPool(5);
        // mThread = new Thread(this);
        // mThread.start();
        mPool.execute(this);
    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 通知关闭线程
        isRunnging = false;
    }

}

你可能感兴趣的:(android,callback,surfaceVie)