1

I'm trying to draw three circles using the canvas in onDraw. onDraw is called by the Thread. But each time canvas.DrawCircle occures, the app crashes.

There's another problem. I'm extending surfaceView, but SurfaceCreated is never called. I think it's not the problem but... I'm going crazy with it. It's my second project and the first one worked well

I tried to debug, You can use it : tag DEBUG. Don't hesitate to ask me the mainActivity if you needs it.

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) {}
        }
    }
}

}

The expected result is three cirles drawn on the activity.

Quentin Girault
  • 41
  • 1
  • 2
  • 8

0 Answers0