如何使用线程在画布上创建图像数组?

时间:2013-02-18 10:34:51

标签: android android-canvas android-animation

我正在开发一个使用不同图像的游戏。我想将该图像设置为背景。并希望在一段时间后更改该图像。看到你无法删除单个图像我想清除画布并从图像数组中重绘图像。我的问题是你如何创建一个可以从onDraw方法访问的数组?我如何在线程中调用onDraw方法以便更改背景。 ?

线程类代码:

private class CanvasThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private boolean isRun = false;

        public CanvasThread(SurfaceHolder holder) {
            this.surfaceHolder = holder;
        }

        public void setRunning(boolean run) {
            this.isRun = run;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Canvas c;

            while (isRun) {
                c = null;
                try {
                    c = this.surfaceHolder.lockCanvas(null);
                    synchronized (this.surfaceHolder) {
                        DrawCanvas.this.onDraw(c);
                        // postInvalidate();

                    }
                } finally {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

public class DrawCanvas extends SurfaceView implements Callback {
    private CanvasThread canvasThread;
    private int cnt = 0;
    private int[] drawable = { R.drawable.freevector_grass_sky1,
            R.drawable.freevector_grass_sky2, R.drawable.freevector_grass_sky3,
            R.drawable.freevector_grass_sky4, R.drawable.freevector_grass_sky5,
            R.drawable.freevector_grass_sky6_5,
            R.drawable.freevector_grass_sky6, R.drawable.freevector_grass_sky7,
            R.drawable.freevector_grass_sky8, R.drawable.freevector_grass_sky9,
            R.drawable.freevector_grass_sky10 };

    public DrawCanvas(Context context) {
        super(context);
        // TODO Auto-generated constructor stub 
    }

    public DrawCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        this.getHolder().addCallback(this);
        this.canvasThread = new CanvasThread(getHolder());
        this.setFocusable(true);
    }

    public DrawCanvas(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub

    }

    public void startDrawImage() {
        canvasThread.setRunning(true);
        canvasThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        boolean retry = true;
        canvasThread.setRunning(false);
        while (retry) {
            try {
                canvasThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stubfor

        Bitmap sweet = BitmapFactory.decodeResource(getResources(),
                drawable[cnt]);
        canvas.drawColor(color.black);
        canvas.drawBitmap(sweet, 0, 0, null);

    }

在我的MainActivity Code中:

if(!isDrawn) {
                DrawCanvas mycanvas = (DrawCanvas)findViewById(R.id.SurfaceView01);
                mycanvas.startDrawImage();
                isDrawn = true;
            }

0 个答案:

没有答案