Category Archives: Uncategorized

Interface Responsiveness

Recently, while working on the Cellular Background, I discovered (again) that doing things in the main interface thread is bad.

In this case, I was working on WallPaperService.Engine.onOffsetsChanged(), which gets calls for each step of the home screen in Android moving. This means that that function gets called many times a second when the user is swiping to the next panel of the home screen. Doing any kind of processing while that is happening is a very bad thing to do, from a user experience prospective.

In the end, I am using something like this:

Class MyWallPaper extends WallPaperService {
   Class MyEngine extends Engine {
       Handler handler;
       Runnable runnable;

        public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
                float yStep, int xPixels, int yPixels)
        {
            this.xPixels = xPixels;
            this.yPixels = yPixels;
            handler.removeCallbacks(runnable);
            handler.post(runnable);
        }
   }
}

The important thing here is that I don’t do any heavy lifting here. The runnable draws the Bitmap I have as a buffer onto the canvas of the WallPaper, which also gets called from the separate Thread that I have doing the lions share of the processing. This lets the main thread send me a message, and I queue further processing to run, and let the interface thread get on with its work.