Im trying to draw a Text on a surfaceview using a canvas. The problem is that the surfaceview doesn't seem to reset after each call. Everything that has been drawn prevously will be drawn again. So, if I have a text that I change position of each time its being drawn, I end up with a long trace of that text because canvas in not cleared.
What am I doing wrong?
public class Test1Activity extends Activity {
private Draw drawText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawText = new Draw(this);
setContentView(drawText);
}
public class Draw extends SurfaceView implements Runnable {
Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
private int i;
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
surfaceHolder = getHolder();
running = true;
thread = new Thread(this);
thread.start();
}
public void run() {
// TODO Auto-generated method stub
while (running) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
Paint paint = new Paint();
paint.setColor(Color.RED);
i++;
if(i > 240)
i = 1;
canvas.drawText("Hello",i , 60, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
public class Test1Activity extends Activity {
private Draw drawText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawText = new Draw(this);
setContentView(drawText);
}
public class Draw extends SurfaceView implements Runnable {
Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
private int i;
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
surfaceHolder = getHolder();
running = true;
thread = new Thread(this);
thread.start();
}
public void run() {
// TODO Auto-generated method stub
while (running) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
Paint paint = new Paint();
paint.setColor(Color.RED);
i++;
if(i > 240)
i = 1;
canvas.drawText("Hello",i , 60, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
Thanks!