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

  • SEARCH
  • Home
  • 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 8370243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:52:09+00:00 2026-06-09T13:52:09+00:00

I used to do my drawing in an ImageView in the onDraw method. However,

  • 0

I used to do my drawing in an ImageView in the onDraw method.
However, I’ve learnt that’s better to draw the canvas outside of the onDraw and just update the canvas in onDraw.

I know this is clearly wrong (because it’s not working) but how would I accomplish what I’m trying to do:

@Override 
public void onDraw(Canvas c) {
  c = this.newCanvas;
  super.onDraw(c);
}
  • 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-09T13:52:11+00:00Added an answer on June 9, 2026 at 1:52 pm
        public class GameLoopThread extends Thread {
    private GameView view;
    private boolean running = false;
    
    public GameLoopThread(GameView view) {
          this.view = view;
    }
    
    public void setRunning(boolean run) {
          running = run;
    }
    
    @Override
    public void run() {
    
          while (running) {
                 Canvas c = null;
    
                 try {
                        c = view.getHolder().lockCanvas();
                        synchronized (view.getHolder()) {
                            if (c != null) {
                                view.onDraw(c);
                            }
    
                        }
                 } finally {
                        if (c != null) {
                               view.getHolder().unlockCanvasAndPost(c);
                        }
                 }
    
                 try {
    
                               sleep(10);
                 } catch (Exception e) {}
          }
    }
    }
    

    make that thread then in your activity do something like this

        @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(new GameView(GameActivity.this));
    }
    

    then in a GameViewClass do something like this

       public class GameView extends SurfaceView {
    
       private SurfaceHolder holder;
       private GameLoopThread gameLoopThread;
    
    
       public GameView(Context context) {
             super(context);
             gameLoopThread = new GameLoopThread(this);
             holder = getHolder();
             holder.addCallback(new SurfaceHolder.Callback() {
    
                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {
                           boolean retry = true;
                           gameLoopThread.setRunning(false);
                           while (retry) {
                                  try {
                                        gameLoopThread.join();
                                        retry = false;
                                  } catch (InterruptedException e) {
                                  }
                           }
                    }
    
                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {
                           gameLoopThread.setRunning(true);
                           gameLoopThread.start();
                    }
    
                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format,
                                  int width, int height) {
                    }
             });
    
       }
    
       @Override
       protected void onDraw(Canvas canvas) {
             //Do Drawing
       }
    }
    

    The important things here is that the thread is manually auto calling the onDraw() method repeatedly, and that you are locking a canvas, drawing on it, and then posting it. If you dont need a super fast refresh rate then you might be better off doing something like this:

        @Override 
        public void onDraw(Canvas c) {
            c = this.getHolder().lockCanvas();
            if (c != null) {
               //draw on canvas
            }
            if (c != null) {
                this.getHolder().unlockCanvasAndPost(c);
            }
        }
    

    I just dont know if that last bit there will work, never tested it.
    also if you want to do your drawing outside the on draw method, you could run your updating (drawing on your canvas) in a thread, and every time the onDraw method is called have it check to see if the Canvas is ready for it to post. for example have your thread have a boolean that once the canvas gets pulled it is set to false, so the thread will draw you a new one, but once it is done drawing set the boolean to true. in the ondraw method check to see if the boolean is true and if it is pull the canvas.

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

Sidebar

Related Questions

We know that for drawing on an image in qt, qpainter is used. Recently,
Does the system come with a standard control that can be used for drawing
just wondering what is behind the major functionalities used in Google Drawing. Does it
Before drawing a shape on a canvas I have a preview that displays how
Is there any Java API (Opensource) that can be used in drawing/creating UML diagrams.
I have used HTML 5 canvas for drawing something on it using javascript after
I'm drawing a line using OpenGL on the iPhone. I used GLView and GLKView
Having used storyboards for a while now I have found them extremely useful however,
I'm drawing rectangles on the screen using the basic NSBezierPath method. I have an
I'm drawing images to pdf using Java framework iText. I need to draw lines

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.