Book Image

Mastering Android Game Development

By : Raul Portales
Book Image

Mastering Android Game Development

By: Raul Portales

Overview of this book

Table of Contents (18 chapters)
Mastering Android Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
API Levels for Android Versions
Index

Adding a frames-per-second (fps) counter


We have updated the DrawThread to run at an arbitrary number of frames per second, adapting to the time required to render instead of a fixed 30 fps, and we are using sprites. Now is the perfect time to add a frames-per-second counter. It is a very easy tool and is also handy to check performance.

We could have used a TextView, but there are some good reasons to draw it on the Canvas directly instead:

  • The performance of SurfaceView suffers when we overlay other views on top of it

  • It is an interesting example of other methods of drawing on the Canvas

  • We can remove and add it without touching the layout

We will make a class named FPSCounter that extends from GameObject and looks like this:

public class FPSCounter extends GameObject {
  private final double mPixelFactor;
  private final float mTextWidth;
  private final float mTextHeight;

  private Paint mPaint;
  private long mTotalMillis;
  private int mDraws;
  private float mFps;

  private String mFpsText...