Android SurfaceView scrolling

Once you start using a SurfaceView, you are moving yourself into a world where you have a blank area of pixels, and you are on your own to draw them how you want. You do everything yourself – and that means you need to implement scrolling yourself as well. It’s not trivial to do, but neither is it too difficult.

One way to do it is the following:
– Extend SurfaceView and implement SurfaceHolder.Callback
– Use getHolder().addCallback( this ) to grab the callback
– Use OnTouch events to implement the scrolling behavior (i.e., when responding, pick up the x,y position from the MotionEvent and set the offset appropriately).

Implement a thread to handle the updating of the image. There are examples of this around on the web (sorry – don’t have any to hand right now).

Alternatively, use an off-screen bitmap and have it drawn by a normal ImageView (size the ImageView to the size of the bitmap and place it in a scroll view).


Edit:

Seeing that this is still getting views, it might be worth pointing out that since this time, I’ve implemented and released as open-source a small library that implements a framework for the above functionality (inspired by many comments here on SO, among others). It can be found on Github: https://github.com/micabyte/android_game

Leave a Comment