0

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!

4

2 回答 2

1

You'll have to ereas the canvas before you draw something eg. canvas.drawColor(Color.BLACK);

  • ereas (draw black)
  • draw text

NB: Don't Create a Paint object in your updating and rendering Thread, it will drive the GC crazy!

于 2012-07-22T15:54:45.873 回答
0

可能您必须以不同的 PorterDuff 模式重新绘制过时的画布。 这个链接很有用。

于 2012-07-22T13:59:08.957 回答