I’m making a simple painting app, and it’s my first time using the canvas. I already have the SurfaceView in my layout xml, and the id is correct. Anyways, here is my code:
package com.example.paint;
import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Paint extends Activity {
/** Called when the activity is first created. */
SurfaceView v;
SurfaceHolder holder;
Canvas c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v = (SurfaceView) findViewById(R.id.svDraw);
holder = v.getHolder();
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!holder.getSurface().isValid())
continue;
c = holder.lockCanvas();
c.drawARGB(255, 0, 255, 0);
holder.unlockCanvasAndPost(c);
}
}
}
Thanks.
You need to remove the
while(true), its an infinite loop that prevents anything from happening.The drawing code belongs in the
onDrawmethod of yourViewthat contains your canvas object