如何在android studio中初始化画布?

时间:2015-07-28 00:59:19

标签: android canvas

我正在尝试在现有类上绘制一个矩形。我见过很多教程都会创建一个新类。但是,我收到一条错误,指出画布未初始化。如何初始化画布?感谢

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    // Get the message from the intent
    Intent intent = getIntent();
    Context context;
    Canvas canvas;
    Rect ourRect = new Rect();
    ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/2);

    Paint blue = new Paint();
    blue.setColor(Color.BLUE);
    blue.setStyle(Paint.Style.FILL);

    canvas.drawRect(ourRect,blue);


}

1 个答案:

答案 0 :(得分:1)

初始化canvas变量,如下所示

   Canvas canvas = new Canvas();