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 8370397
In Process

The Archive Base Latest Questions

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

I’m using a SurfaceView and I have to update it as fast as I

  • 0

I’m using a SurfaceView and I have to update it as fast as I can. Since the onDraw method isn’t called automatically I have to call explicitly. So my first attempt was on threads, but I got some lag. There is a better way to do this? If there is a better solution, can you, please, explain me? I’m new on Android’s World!


What I have so far

public void run() {
    while(running){
        Canvas c = null;
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(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:54:10+00:00Added an answer on June 9, 2026 at 1:54 pm

    Here’s my code that I use as a rendering thread. The fps is updated frame dependent. I usually run my games at 30 fps.
    (There’s some code you won’t need, but I’m sire you’ll be able to find the pieces you are looking for.)

    public class RenderView extends SurfaceView implements Runnable {
    
    
        Game game;
        Bitmap gameScreen;
        Thread gameloop;
        SurfaceHolder holder;
        boolean running;
    
        int sleepTime;
        int numberOfFramesSkipped;
        int maxFrameSkips;
        long beginTime;
        long endTime;
        long lastTime;
        int differenceTime;
        int framePeriod;
        Canvas frameBuffer;
        int frameCount;
    
        public RenderView(int fps, int maxFrameSkips) {
            super(game);
            this.gameScreen = gameScreen;
            this.holder = getHolder();
            this.framePeriod = 1000/fps;
            this.maxFrameSkips = maxFrameSkips;
            lastTime = System.currentTimeMillis();
            beginTime = System.currentTimeMillis();
        }
    
        @Override
        public void run() {
    
            while(running == true) {
                if(holder.getSurface().isValid()) {
    
                    beginTime = System.currentTimeMillis();
    
                        // call your update method here
                    // render here
                        frameBuffer = holder.lockCanvas();
                        this.onDraw();
                        holder.unlockCanvasAndPost(frameBuffer);
    
                    // Frame Per Second Count
                    frameCount++;
    
                    if(lastTime + 1000 < System.currentTimeMillis()) {
                        lastTime = System.currentTimeMillis();
                        frameCount = 0;
                    }
    
                    endTime = System.currentTimeMillis();;
                    differenceTime = (int) (endTime - beginTime);
                    sleepTime = (int) (framePeriod - differenceTime);
    
                    if(sleepTime > 0) {
                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException exception) {
                            exception.printStackTrace();
                        }
                    }
                    else {
                        while(sleepTime < 0 && numberOfFramesSkipped < this.maxFrameSkips) {
                            // Call your update method here
                            sleepTime += framePeriod;
                            numberOfFramesSkipped++;
                        }
                    }
    
    
                }
    
    
            }
    
        }
    
        private void renderFrameBuffer() {
            // Update the current virtual screen image
            game.getCurrentScreen().render();
            // Render the current virtual screen to the real phone screen
            frameBuffer = holder.lockCanvas();
            if(frameBuffer != null) { // Fix for mysterious bug ( FATAL EXCEPTION: Thread)
                // The viewing area of our virtual screen on our real screen
                // Composition
                // First layer (bottom)
                    frameBuffer.drawBitmap(this.gameScreen, null, game.getWSScreen().getGameScreendst(), null);
                // Second layer (top)
                    frameBuffer.drawBitmap(this.gameScreenExtended, null, game.getWSScreen().getGameScreenextendeddst(), null);
    
                holder.unlockCanvasAndPost(frameBuffer);
            }
            else {
                gameEngineLog.e(classTAG, "Surface has not been created or otherwise cannot be edited");
            }
        }
    
        public void resume() { 
            this.running = true;
            gameloop = new Thread(this);
            gameloop.start();         
        }   
    
        public void pause() { 
            this.running = false;
            running = false;                        
            while(true) {
                try {
                    gameloop.join();
                    break;
                } catch (InterruptedException e) {
                    // retry
                }
            }
        }
    
    }
    
    public void onDraw() {
    
    }
    

    I suggest you take a look at this link or have a look at
    http://code.google.com/p/beginning-android-games/
    or http://www.edu4java.com/en/androidgame/androidgame3.html

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the

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.