Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using SurfaceView to draw a lot of Bitmaps onto canvas. This causes really variable frame rates depending on how many objects are present. My code below:

public class OriginalView extends SurfaceView implements SurfaceHolder.Callback {
  private final static int THREAD_SLEEP_TIME = 32;
  private DrawThread drawthread;

  private onDraw(Canvas canvas);
  public void onSizeChanged(int w, int h, int oldW, int oldH);
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height);
  public void surfaceCreated(SurfaceHolder holder);
  public void surfaceDestroyed(SurfaceHolder holder);

  private DrawThread extends Thread {
    private SurfaceHolder surfaceHolder;
    public boolean threadIsRunning;
    public DrawThread (SurfaceHolder holder) {
      threadIsRunning = true;
      surfaceHolder = holder;
    }
    public void run() {
      Canvas canvas = null;
      while(threadIsRunning) {
        try {
          canvas = surfaceHolder.lockCanvas(null);
          synchronized (surfaceHolder) {
            if (canvas!=null) onDraw(canvas);
          }

          sleep(THREAD_SLEEP_TIME);

        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          if (canvas != null) surfaceHolder.unlockCanvasAndPost(canvas);
        }
      }
    }
  }
}

As you can see here in DrawThread's run function that the Thread is supposed to sleep for a constant value of time, predefined here as 32 milliseconds, but since sometimes onDraw(canvas) takes more time, varied due to number of components on screen, the frame rate per second that this code achieve is 17 - 51 fps in my game. This is highly unacceptable. How can I draw lots of Bitmaps onto a canvas, while maintaining high frame rates?

share|improve this question
    
The correct way to do this is to post the question and answer separately. If you could please edit the question to remove the answer, then post the answer below that would be appreciated. I know you have a waiting period, so after that time is fine. Thanks for sharing! –  Byte56 Aug 6 '13 at 22:25
    
I'll just do that in my next post. Thanks for the suggestion. I'm still new to the site. –  Aekasitt Guruvanich Aug 12 '13 at 15:27
    
I removed the answer for you, feel free to add it as an answer below. –  Byte56 Aug 12 '13 at 16:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.