如何使用invalidate()metod重绘SurfaceView?

时间:2013-02-03 20:07:33

标签: android android-canvas android-view

我有一个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);
            }
        }

    }

pawnBoardbigPawnsmallPawn在此课程中公开,它们就像:

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方法确实更改了图像,我使用setBitmaps()变量进行了检查,这是正确的。但无效功能不起作用。为什么? 以下是我调用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);


    }

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

1 个答案:

答案 0 :(得分:2)

覆盖surfaceCreated()。然后添加以下行:

setWillNotDraw(false);