如何使用无效()梅托德重绘SurfaceView? [英] How to redraw SurfaceView using invalidate() metod?
问题描述
我有一个类游戏键盘
延伸 SurfaceView
。里面我有一个的OnDraw
方法:
I have a class GameBoard
which extends SurfaceView
. Inside I have an OnDraw
method:
@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);
}
}
}
pawnBoard
, bigPawn
和 smallPawn
是市民这个类,他们是这样的:
pawnBoard
, bigPawn
and smallPawn
are public in this class and they are like:
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的图像的第二种方法:
I have also a second method in which I'm changing the image of bigPawn and 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
方法:
But after calling this method inside another (inside another class), the images are not changing. I checked using the tmp
variable if setBitmaps()
method really changes the images, and this is correct. But the invalidate function doesn't work. Why?
Here is how I'm calling the setBitmaps
method:
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);
}
我一直在寻找解决方案,但这些都不是我发现是有益的。
I was looking for the solution, but none of this which I found was helpfull.
推荐答案
覆盖 surfaceCreated()
。然后,加入这一行:
Override surfaceCreated()
. Then add this line:
setWillNotDraw(false);
这篇关于如何使用无效()梅托德重绘SurfaceView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!