Moving objects in a 2D game is very easy in an Android 2D game, we know. Just use a SurfaceView and draw the single bitmaps with Canvas.drawBitmap().
But when it comes to a rolling object, e.g. a ball, how can I achieve this? Using transformation matrices results in very poor quality of the rendered bitmap, doesn’t it?
If you want to rotate a bitmap using Canvas based drawing, you don’t have much choice but to use transformation matrices, either through a Matrix you pass to Canvas.drawBitmap() or through transformation operations invoked directly on Canvas. And if you enable bitmap filtering with Paint.setFilterBitmap(boolean) the quality will be fine, at least by my standards.
Here is a complete example (Matrix-based) you can play around with, in particular looking at what difference it makes to have bitmap filtering on and off (or just look at my screenshots below). It doesn’t use SurfaceView though, just a normal custom View, but it should be easy to port to SurfaceView:
CustomView.java:
ExampleActivity.java:
Sample screenshot with bitmap filtering enabled. If you run the example, the bitmap will roll like a ball (excuse my poor choice of ball graphics):
Sample screenshot with bitmap filtering disabled.