1

onDraw のキャンバスを使用して 3 つの円を描画しようとしています。onDraw はスレッドによって呼び出されます。しかし、canvas.DrawCircle が発生するたびに、アプリがクラッシュします。

別の問題があります。surfaceView を拡張していますが、SurfaceCreated が呼び出されることはありません。それは問題ではないと思いますが...私はそれに夢中になります。これは私の 2 番目のプロジェクトで、最初のプロジェクトはうまくいきました

私はデバッグしようとしました, あなたはそれを使うことができます: タグ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) {}
        }
    }
}

}

期待される結果は、アクティビティに描かれた 3 つの円です。

4

0 に答える 0