Skip to main content
additional info
Source Link

EDIT:

You should definitely handle the drawing in a separate thread from the user input. Also depending on what version of Android you are building for, you might want to sleep in the input handling method for a couple milliseconds. You would want to do this because in earlier versions of Android, pressing the screen would result in a flood of inputs which would slow down the processing of an application. This was more acute in games. Check out the Replica Island code for more information on what I'm talking about.

Specifically if you look at: http://code.google.com/p/replicaisland/source/browse/trunk/src/com/replica/replicaisland/AndouKun.java on line: 344

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mGame.isPaused()) {
                mGame.onTouchEvent(event);
                
                final long time = System.currentTimeMillis();
                if (event.getAction() == MotionEvent.ACTION_MOVE && time - mLastTouchTime < 32) {
                        // Sleep so that the main thread doesn't get flooded with UI events.
                        try {
                            Thread.sleep(32);
                        } catch (InterruptedException e) {
                            // No big deal if this sleep is interrupted.
                        }
                        mGame.getRenderer().waitDrawingComplete();
                }
                mLastTouchTime = time;
        }
        return true;
    }

Chris sleeps the input thread for 32 milliseconds which is approximately 30 sleeps per second (which matches 30 fps for the game). This makes sense because if you are only calling your update method once per frame, you don't need to have the input thread produce any more than 1 input for a touch event.

EDIT:

You should definitely handle the drawing in a separate thread from the user input. Also depending on what version of Android you are building for, you might want to sleep in the input handling method for a couple milliseconds. You would want to do this because in earlier versions of Android, pressing the screen would result in a flood of inputs which would slow down the processing of an application. This was more acute in games. Check out the Replica Island code for more information on what I'm talking about.

Specifically if you look at: http://code.google.com/p/replicaisland/source/browse/trunk/src/com/replica/replicaisland/AndouKun.java on line: 344

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mGame.isPaused()) {
                mGame.onTouchEvent(event);
                
                final long time = System.currentTimeMillis();
                if (event.getAction() == MotionEvent.ACTION_MOVE && time - mLastTouchTime < 32) {
                        // Sleep so that the main thread doesn't get flooded with UI events.
                        try {
                            Thread.sleep(32);
                        } catch (InterruptedException e) {
                            // No big deal if this sleep is interrupted.
                        }
                        mGame.getRenderer().waitDrawingComplete();
                }
                mLastTouchTime = time;
        }
        return true;
    }

Chris sleeps the input thread for 32 milliseconds which is approximately 30 sleeps per second (which matches 30 fps for the game). This makes sense because if you are only calling your update method once per frame, you don't need to have the input thread produce any more than 1 input for a touch event.

Source Link

You might want to take a look at the Android Examples, specifically JetBoy:

http://developer.android.com/resources/samples/JetBoy/index.html

I believe they have scrolling backgrounds, specifically Parallax Scrolling (which you can look up here: http://en.wikipedia.org/wiki/Parallax_scrolling)

But if you just want one background scroll, you could get a pretty good feel for how to do it from this game example.

(NOTE: I believe this game has scrolling backgrounds, correct me if I am wrong and I will edit this Answer)