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 itIt 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...