我创建了两个类 - 一个是扩展SurfaceView
并具有方法的类onDraw()
,在其中我在画布上绘制图像:
public class Board extends SurfaceView{
public BitmapFactory myBitmapFactory = new BitmapFactory();
public Bitmap myBitmap = new Bitmap;
public Board(Context context) {
super(context);
setFocusable(true);
}
//here i implement all the abstract classes, which are not important now
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
Paint paint = new Paint();
canvas.drawBitmap(myBitmap, 20, 20, paint);
}
public void setBitmap(Bitmap b){
this.myBitmap = b;
}
}
而且我还有我的活动类,在单击运行该方法的按钮后changeImage
。我想要两个更改我放在画布上的图像
public class MyActivity extends Activity {
public Context context = this;
public Board board;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_game);
}
public void changeImage(View view){
Bitmap tmpBitmap = new Bitmap();
BitmapFactory tmpBitmapFactory = new BitmapFactory();
tmpBitmap = Bitmap.createScaledBitmap(tmpBitmapFactory.decodeResource(getResources(), R.drawable.image2), 130, 130, false);
board.setBitmap(tmpBitmap);
board.invalidate();
}
}
但是在调用方法之后changeImage
什么都没有改变 - 图像与开始时相同。我也尝试使用invalidate()
insidesetBitmap()
方法,但它也不起作用。这是因为每次我打电话时invalidate()
我都在创建一个新的位图还是我做错了什么?
---编辑---所以,错误的是:
myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
一直在设置相同的图像。我改变了它,但仍然 invalidate() 那些不起作用。我的意思是图像没有改变。我在其他主题中寻找结果,但我没有找到任何解决方案,现在我的课程看起来像这样:
public class Board extends SurfaceView{
public BitmapFactory myBitmapFactory = new BitmapFactory();
public Bitmap myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), size, size, false);
public int tmp = 0;
public Paint paint = new Paint();
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(myBitmap, 50, 50, paint);
}
public void setBitmaps(Bitmap one){
this.paint = new Paint();
this.myBitmap = one;
this.tmp=4;
this.invalidate();
}
我的活动中的方法 changeImage() :
public void changeImage(View view){
Bitmap tmpBitmap1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.blue), 230, 230, false);
myGameBoard.setBitmaps(tmpBitmap1);
}
我尝试使用:invalidate()、postInvalidate(),但它不起作用,我现在知道我做错了什么