在onDraw上调用canvas.drawCircle时,应用程序崩溃

时间:2019-01-29 19:39:05

标签: java android android-studio android-canvas android-thread

我正在尝试使用onDraw中的画布绘制三个圆圈。 onDraw由线程调用。 但是每次canvas.DrawCircle发生时,应用程序就会崩溃。

还有另一个问题。我正在扩展surfaceView,但从未调用SurfaceCreated。我认为这不是问题,但是... 我要疯了。这是我的第二个项目,第一个很好用

我尝试调试,可以使用它:标记DEBUG。 请不要犹豫问我mainActivity。

public class LabyrintheView extends SurfaceView implements 
SurfaceHolder.Callback {

SurfaceHolder mSurfaceHolder;
DrawingThread mThread;
private byte[] tabRaw;
private Bitmap bitmapScaled;
static final int imageH=72;
static final int imageW=48; 
public int level;

static final int wallColor = 0xFF000000;
static final int trapColor = 0xFFFF4500;
private float ballX, ballY; private float ballSpeedY, ballSpeedX;
private float startX, startY, endX, endY;
private float radius;
public Paint paintBall, paintStart, paintEnd;
private Point screenSize;

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

    mSurfaceHolder = getHolder();
    mSurfaceHolder.addCallback(this);
    screenSize=new Point();
    screenSize.x=1080;
    screenSize.y=1776;
    radius=10;
    tabRaw = new byte[imageH*imageH];

    mThread= new DrawingThread();
    Resources res = getResources();
    InputStream istr=res.openRawResource(R.raw.lab2);

    switch(level)
    {
        case 2 : istr= res.openRawResource(R.raw.lab2); break;
        case 3 : istr= res.openRawResource(R.raw.lab3); break;
        default: istr= res.openRawResource(R.raw.lab1); break;
    }
    try{
        istr.read(tabRaw,0,imageH*imageW);
        istr.close();
    }catch(IOException e){
        e.printStackTrace();
    }
    int tabInt[] = new int[imageH*imageW];

    for(int i=0;i<imageW*imageH; i++)
    {
        switch(tabRaw[i])
        {
            case 1:
                tabInt[i] = wallColor;
                break;
            case 2:
                ballX=startX=i%imageW;
                ballY=startY=(i-ballX)/imageW;
                break;
            case 3:
                endX=i%imageW;
                endY=(i-endX)/imageW;
                break;
            case 4:
                tabInt[i] = trapColor;
                break;
            default:
                break;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(imageW,imageH, Bitmap.Config.ARGB_8888);
    Log.d("DEBUG","| LabyrintheView | createBitmap");
    bitmap.setPixels(tabInt,0,imageW,0,0,imageW,imageH);
    Log.d("DEBUG","| LabyrintheView | setPixels");
    bitmapScaled= Bitmap.createScaledBitmap(bitmap, screenSize.x, screenSize.y, false);
    Log.d("DEBUG","| LabyrintheView | createScaledBitmap");
    //bitmapScaled=bitmap;

    float ratioX=((float)screenSize.x)/((float)imageW);
    float ratioY=((float)screenSize.y)/((float)imageH);
    ballX=startX;//=startX*ratioX + ratioX/2;
    ballY=startY;//=startY*ratioY + ratioX/2;
    //endX=endX*ratioX+ ratioX/2;
    //endY=endY*ratioY+ ratioX/2;
//launching manually the thread as SurfaceCreated is not called
    mThread.keepDrawing=true;
    mThread.start();
    Log.d("DEBUG","| LabyrintheView | onCreate");
}

@Override
protected void onDraw(Canvas canvas) {

    Paint paintBall, paintStart, paintEnd;
    paintBall=new Paint();
    paintBall.setColor(Color.BLUE);

    paintStart=new Paint();
    paintStart.setColor(Color.RED);
    paintEnd= new Paint();
    paintEnd.setColor(Color.GREEN);

    canvas.drawCircle(endX, endY,radius, paintEnd);

    canvas.drawCircle(100, 100, radius, paintBall);
    Log.d("DEBUG","| LabyritheView | onDraw");
    canvas.drawCircle(startX, startY, radius, paintStart);
    super.onDraw(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder pHolder) {
    mThread.keepDrawing=false;
    Log.d("DEBUG","| LabyrintheView | surfaceDestroyed");
}
@Override
public void surfaceChanged(SurfaceHolder pHolder, int pFormat, int pWidth, int pHeight) { Log.d("DEBUG","| LabyrintheView | surfaceChanged");}
@Override
public void surfaceCreated(SurfaceHolder pHolder){
    Log.d("DEBUG","| LabyrintheView | surfaceCreated");
    mThread.keepDrawing=true;
    mThread.start();

}

private class DrawingThread extends Thread {
    boolean keepDrawing = true;

    @Override
    public void run() {
        Log.d("DEBUG","| DrawingThread | run");
        Canvas canvas;
        while (keepDrawing) {
            canvas = null;

            try {

                canvas = mSurfaceHolder.lockCanvas();
                synchronized (mSurfaceHolder) {

                    onDraw(canvas);
                }
            } finally {
                if (canvas != null)
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
            }

            // Pour dessiner à 50 fps
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {}
        }
    }
}

}

预期结果是在活动中绘制了三个圆。

0 个答案:

没有答案