Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:10:53+00:00 2026-06-08T04:10:53+00:00

Im trying to draw a Text on a surfaceview using a canvas. The problem

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:10:54+00:00Added an answer on June 8, 2026 at 4:10 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw text using Core Text functions, with a line spacing that's
I'm trying to draw text using canvas. I checked out everywhere but those examples
I'm trying to draw vertical text in win32 GDI api. I call CreateFont() with
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw text without antialiasing on a canvas in Android. The font
I am trying to draw text on a live wallpaper that has a background
I'm trying to draw text on NSView subclass. I'm using an NSTextStorage, an NSLayoutManager,
I'm trying to draw a String inside a canvas. The text and the position
I'm trying to render some text using PIL, but the result that comes out
Possible Duplicate: Draw multi-line text to Canvas I'm trying to draw text to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.