0

我创建了一个扩展 SurfaceView 的类 XYZ。在 onDraw 方法中,我创建了一个包含图像的位图数组,然后使用方法 drawBitmap 将图像放置在我的画布上。这看起来或多或少是这样的:

public class Board extends SurfaceView{


   public BitmapFactory myBitmapFactory = new BitmapFactory();
   public  Bitmap myBitmap = new Bitmap();
   protected void onDraw(Canvas canvas) {
           myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), size, size, false);

Paint paint = new Paint();
canvas.drawBitmap(myBitmap, x, y, paint);

我还有第二节课,它是 MyActivity,单击按钮后我想更改画布上的图像:

public class MyActivity extends Activity {
public Context context = this;
     public Board myGameBoard;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_game);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
}

public void changeImage(View view){
   //here I want to change the image
   }
}

(我只发布了最重要的代码行,我认为它们负责添加图像)。

但我不知道如何到达我在 onDraw 方法中使用的画布,因为它是一个局部变量。我应该怎么做才能改变图像?

4

1 回答 1

1

为您的班级添加一个新的二传手Board,例如setBitmap(Bitmap). 将位图引用保存在那里并使视图无效,以便onDraw()再次调用。在onDraw读取设置的位图。完毕!

于 2013-02-03T00:36:29.117 回答