0

在模拟器和手机上看起来都是这样。它总是一个被破坏的随机方块。

这是 SurfaceView 类的代码,最相关的部分是 prepareBackground() :

代码开始:

公共类 MenuBackgroundView 扩展 SurfaceView 实现 SurfaceHolder.Callback {

private Paint mPaint;
private Bitmap blocks12x12;
private boolean draw = false;
private Context ctx;
//private int begOffX;
private int offX;
private int offY;
private int mWidth = 1;
private int mHeight = 1;
private ViewThread mThread;
private int calcSizeXY;

public MenuBackgroundView(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
    mPaint = new Paint();
    mPaint.setColor(Color.RED);
    ctx = context;
}




public void doDraw(long elapsed, Canvas canvas) {

    canvas.drawColor(Color.BLUE);

    if(draw)
    {
        canvas.drawBitmap(blocks12x12, offX, offY, mPaint);
    }
    canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: ", 10, 10, mPaint);
}

public void animate(long elapsed)
{
    /*
    //elapsed = elapsed/10;
    offX = (offX+2);
    if(offX >= 0)
    {
        offX -= 2*calcSizeXY;
    }

    offY = (offY+3);
    if(offY >= 0)
    {
        offY -= 2*calcSizeXY;
    }
    //offY = (offY + (int)(elapsed/10f)) % calcSizeXY*2;//
     * 
     *///
}

public void prepareBackground()
{
    if(mWidth <= 1 || mHeight <= 1 )
        return;

    Log.d("Menu", "prepareBackground");

    if(mHeight > mWidth)
    {
        calcSizeXY = mHeight/10;
    }
    else
    {
        calcSizeXY = mWidth/10;     
    }

    offX = -2*calcSizeXY;

    Bitmap block = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.block);
    block = Bitmap.createScaledBitmap(block, calcSizeXY, calcSizeXY, false);




    // Group together
    int sizeX = 12*calcSizeXY;
    int sizeY = 12*calcSizeXY;
    blocks12x12 = Bitmap.createBitmap(sizeX, sizeY, Bitmap.Config.ARGB_8888);
    Canvas blocks12x12Canvas = new Canvas(blocks12x12);

    for(int i = 0; i < 14; i+=2)
    {
        for(int j = 0; j < 14; j+=2)
        {
            blocks12x12Canvas.drawBitmap(block, (j*calcSizeXY), (i*calcSizeXY), mPaint);
        }
    }

    // "Memory leak"
    block.recycle();

    draw = true;
}




@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    mWidth = width;
    mHeight = height;
    prepareBackground();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    prepareBackground();
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
        freeMem();
    }
}




public void freeMem() {
    // TODO Auto-generated method stub
    draw = false;
    blocks12x12.recycle(); // "Memory leak"
}

}

4

1 回答 1

0

好的,我想通了,只需让线程休眠 1ms:

blocks12x12 = Bitmap.createBitmap(sizeX, sizeY, Bitmap.Config.ARGB_8888);
    Canvas blocks12x12Canvas = new Canvas(blocks12x12);

    for(int i = 0; i < 14; i+=2)
    {
        for(int j = 0; j < 14; j+=2)
        {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            blocks12x12Canvas.drawBitmap(block, (j*calcSizeXY), (i*calcSizeXY), mPaint);
        }
    }
于 2012-04-23T21:19:36.207 回答