Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a viewport on a SurfaceView

 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How can I create or simulate in an Android application something like LayerManager.setViewWindow or Swing JViewport?

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?

Thanks.

- Sonny
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Link - Creating a Viewport in Android
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic