Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从一个类访问另一个类中的画布_Android_Android Canvas_Android View - Fatal编程技术网

Android 从一个类访问另一个类中的画布

Android 从一个类访问另一个类中的画布,android,android-canvas,android-view,Android,Android Canvas,Android View,我创建了一个扩展SurfaceView的类XYZ。在onDraw方法中,我创建了一个带有图像的位图数组,然后使用drawBitmap方法将图像放置在画布上。这看起来或多或少像这样: public class Board extends SurfaceView{ public BitmapFactory myBitmapFactory = new BitmapFactory(); public Bitmap myBitmap = new Bitmap(); protecte

我创建了一个扩展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方法中使用的画布,因为它是一个局部变量。如何才能更改图像?

类添加一个新的setter,例如
setBitmap(位图)
。保存位图引用并使视图无效,以便再次调用
onDraw()
。在
onDraw
中读取已设置的位图。完成了