将canvas传递给onDraw方法中的函数

时间:2014-02-09 06:20:43

标签: android canvas view

有两个类:Renderer,它扩展了View和Grid。在Renderer的onDraw方法中,我想将Canvas传递给Grid的函数。由于Java仅通过值传递参数,因此下面的代码不起作用。我的意思是它不会在屏幕上绘制一个矩形,并说该程序已停止在我的手机上。有什么想法吗?

public class Renderer extends View {

Paint paint;
Grid grid;

public Renderer(Context context, Grid grid) {
    super(context);
    paint = new Paint();
    this.grid = grid;
}

protected void onDraw (Canvas canvas) {
    canvas.drawRGB(255, 255, 255);      
    grid.tempName(canvas); // removing this solves problem
    invalidate();
}
}

public class Grid { 

Paint paint;

public void tempName (Canvas canvas) {

    paint.setStyle(Style.FILL);
    paint.setColor(Color.RED);
    canvas.drawRect(100, 100, 200, 200, paint);
}   
}

0 个答案:

没有答案