1

我创建了两个类 - 一个是扩展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(),但它不起作用,我现在知道我做错了什么

4

1 回答 1

3

You update myBitmap in onDraw():

myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);

So your updated image is still not used as you directly override it with another image...


(old answer, which is now obsolete due to fixed question)
There seems something else off in your example code. In setBitmap() you store the new bitmap in pawnBitmap. In the onDraw() method, you do not use pawnBitmap. So it is expected that nothing changes, as you do not use the changed values...


(old answer, which is now obsolete due to change in question)
You are trying to call a static class method with:

Board.setBitmap(tmpBitmap);
Board.invalidate();

(I am wondering why you did not get compile errors... Or did you forgot to mention them?)
As Board is the name of your class. Instead you need to call the instance method.

Board board = ...
board.setBitmap(tmpBitmap);
board.invalidate();

This obviously requires the board instance, which you need to available in your activity class.

Note: if you are calling invalidate() from a non-UI thread, you need to use postInvalidate().

于 2013-02-03T14:47:27.397 回答