File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Android and the fly likes Creating a viewport on a SurfaceView Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Creating a viewport on a SurfaceView" Watch "Creating a viewport on a SurfaceView" New topic

Creating a viewport on a SurfaceView

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211


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?


- Sonny

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

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 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
I agree. Here's the link:
subject: Creating a viewport on a SurfaceView
It's not a secret anymore!