0

我有一个简单的surfaceView 和一个线程,如下所示。

我试图从简单的开始,只是将surfaceView的背景绘制成某种颜色。

package com.example.surfviewtest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SView extends SurfaceView implements SurfaceHolder.Callback {

    private Paint backgroundPaint;
    private Activity activity;
    private Paint circlePaint;
    private vThread vThread;
    private Canvas canvas;

    public SView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        activity = (Activity) context;

        getHolder().addCallback(this);

        backgroundPaint = new Paint();
        circlePaint = new Paint();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        backgroundPaint.setColor(Color.YELLOW);
        circlePaint.setStrokeWidth(w / 12);
        circlePaint.setColor(Color.BLUE);

        vThread = new vThread(getHolder());
        vThread.start();

        // Canvas canvas = null;
        // SurfaceHolder surfaceHolder = null;
        // canvas = surfaceHolder.lockCanvas(null);
        // canvas.drawRect(0, 0,canvas.getWidth(),canvas.getHeight(),
        // backgroundPaint);
    }

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

    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    public void svDraw(Canvas canvas){
        canvas.drawRect(0, 0,canvas.getWidth(),canvas.getHeight(), backgroundPaint);
    }

    private class vThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private boolean threadRunning = true;

        public vThread(SurfaceHolder holder) {
            surfaceHolder = holder;
            setName("vThread");
        }

        @Override
        public void run() {
            Canvas canvas = null;
            while (threadRunning) {
                try {
                    canvas = surfaceHolder.lockCanvas(null);

                    synchronized (surfaceHolder) {

                         svDraw(canvas);
                    }
                } finally {
                    if (canvas != null)
                        surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

}

但是这条线让我的应用程序在我运行它时崩溃:

canvas.drawRect(0, 0, canvas.getWidth(),canvas.getHeight(), backgroundPaint);

我在构造函数中定义了 backgroundPaint,并在 onSizeChanged 中设置了它的颜色。还有什么需要做的吗?

4

1 回答 1

0

我想知道你锁定画布但从不解锁它来绘制它的那条线上方...... 因此,请尝试在该行之后立即解锁它。

于 2013-06-07T15:56:55.833 回答