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 という 2 番目のクラスがあります。
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 メソッド内で使用していたキャンバスにアクセスする方法がわかりません。画像を変更できるようにするにはどうすればよいですか?