1

我有一个GameBoard扩展类SurfaceView。在里面我有一个OnDraw方法:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size  = getWidth()/8;
        canvasSize = size;
        pawnBitmap[0] = bigPawn;
        pawnBitmap[1] = smallPawn;
        float left_distance;
        float top_distance;
        for(int j = 0; j < 10; j++){
            for (int i =0; i < 4; i++){
                left_distance = i*size+(float)50;
                top_distance = j*size+(float)10;
                canvas.drawBitmap(pawnBitmap[0], left_distance, top_distance, paint);
                canvas.drawBitmap(pawnBitmap[1], 6*size+i*30+(float)10, top_distance+30, paint);
            }
        }

    }

pawnBoardbigPawn并且smallPawn在这个类中是公开的,它们就像:

public BitmapFactory myBitmapFactory = new BitmapFactory();

public  Bitmap[] pawnBitmap = new Bitmap[2];
public Bitmap bigPawn =  Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.game_button_2), 90, 90, false);
public Bitmap smallPawn = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.game_button_2), 30, 30, false);

我还有第二种方法,我正在更改 bigPawn 和 smallPawn 的图像:

public void setBitmaps(Bitmap one, Bitmap two){
        this.paint = new Paint();
        this.bigPawn = one;
        this.smallPawn = two;
        this.tmp=4;
        invalidate();

    }

但是在另一个(在另一个类中)调用这个方法之后,图像并没有改变。我检查了使用tmp变量 ifsetBitmaps()方法是否真的改变了图像,这是正确的。但是无效功能不起作用。为什么?这是我调用该setBitmaps方法的方式:

public void savePermutation(View view){

        Bitmap tmpBitmap1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.blue), 230, 230, false);
        Bitmap tmpBitmap2 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.blue), 100, 100, false);
        myGameBoard.setBitmaps(tmpBitmap1, tmpBitmap2);


    }

我一直在寻找解决方案,但我发现这些都没有帮助。

4

1 回答 1

2

覆盖surfaceCreated()。然后添加这一行:

setWillNotDraw(false);
于 2013-02-03T21:00:21.430 回答