This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am writing a PacMan like game, and if the central character moves off screen, I need to follow it.
This is easily done using the standard components in J2ME or Swing, but I am having a hard time figuring out the best way to do it on Android.
My screen consists of a SurfaceView subclass (plus other stuff around it set in the layout xml), and I draw everything manually in code. I can think of a couple of ways of doing this. One would be to calculate how much of the game screen will be shown, and start the drawing from an appropriate point of origin.
Another will be to draw the whole screen in some kind of off screen buffer and only show enough of it to keep the character in view. This will consume more memory and a little more processing, but it won't be much and should be acceptable if it results in easier to read code.
But both these approaches need me to do it manually, is there an easier, standard way of doing it in Android?
It took me a while to get it working properly. My bad for not reading the docs properly first
In case anyone else needs something similar, it is easily done using android.graphics.Canvas.translate() method.
If you are creating a custom View, also have a close look at the android.view.View.onMeasure() method.
Using the functionality exposed by these two methods, it is quite easy to implement the viewport like behavior.