05 March 2009
Http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html
Window backgrounds & UI speed
Some Android applications require to squeeze every bit of performance out of the UI toolkit and there are always ways to do so. In this article, you will discover how to speed up the drawing and
PerceivedStartup time of your activities. Both these techniques rely on a single feature, the window's background drawable.
The termWindow backgroundIs a bit misleading however. When you setup your user interface by calling
setContentView()
On
Activity, Android adds your views toActivity
'S window. the window however does not contain only your views, but a few others created for you. the most important one is, in the current implementation used on the T-Mobile G1,
DecorView
, Highlighted in the view hierarchy below:
TheDecorView
Is the view that actually holds the window's background drawable. Calling
Getwindow (). setbackgrounddrawable () from yourActivity
Changes the background of the window by changing
DecorView
'S background drawable. As mentioned before, this setup is very specific to the current implementation of Android and can change in a future version or even on another device.
If you are using the standard Android themes, a default background drawable is set on your activities. The standard theme currently used on the T-Mobile G1 uses for instance
Colordrawable. for most applications, this background drawable works just fine and can be left alone. it can however impacts your application's drawing performance. let's take the example of an application that always draws a full screen opaque picture:
You can see on this screenshot that the window's background is invisible, entirely covered by
ImageView
. This application is setup to redraw as fast as it can and draws at about 44 frames per second, or 22 milliseconds per frame (Note:The number of frames per second used in this article were obtained on a T-Mobile G1
My finger on the screen so as to reduce the drawing speed which wowould otherwise be capped at 60 FPS.) an easy way to make such an application draw faster is
RemoveThe background drawable. Since the user interface is entirely opaque, drawing the background is simply wasteful. Removing the background improves the performance quite nicely:
In this new version of the application, the drawing speed went up to 51 frames per second, or 19 milliseconds per frame. the difference of 3 milliseconds per is easily explained by the speed of the memory bus on the T-Mobile G1: It is exactly the time it
Takes to move the equivalent of a screenful of pixels on the bus. The difference cocould be even greater if the default background was using a more expensive drawable.
Removing the window's background can be achieved very easily by using a custom theme. To do so, first create a file called
res/values/theme.xml
Containing the following:
<resources> <style name="Theme.NoBackground" parent="android:Theme"> <item name="android:windowBackground">@null</item> </style></resources>
You then need to apply the theme to your activity by adding the attribute
android:theme="@style/Theme.NoBackground"
To your<activity />
Or
<application />
Tag. This trick comes in very handy for any app that uses
MapView
,WebView
Or any other full screen opaque view.
Opaque views and Android: This optimization is currently necessary because the android UI toolkit is not smart enough to prevent the drawing of views hidden by opaque children. The main reason why this optimization was not implemented is
Simply because there are usually very few opaque views in Android applications. this is however something that I definitely plan on implementing as soon as possible and I can only apologize for not having been able to do this earlier.
Using a theme to change the window's background is also a fantastic way to improve
PerceivedStartup Performance of some of your activities. This particle trick can only be applied to activities that use a custom background, like a texture or a logo.
Shelves application is a good example:
If this application simply set the wooden background in the XML layout or in
onCreate()
The user wocould see the application startup with the default theme and its dark background. the wooden texture wocould only appear after the inflation of the content view and the first layout/drawing pass. this causes a jarring effect and gives
The user the impression that the application takes time to load (which can actually be the case .) instead, the application defines the wooden background in a theme, picked up by the system as soon as the application starts. the user never sees the default
Theme and gets the impression that the application is up and running right away. To limit the memory and disk usage, the background is a tiled texture defined in
res/drawable/background_shelf.xml
:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/shelf_panel" android:tileMode="repeat" />
This drawable is simply referenced by the theme:
<resources> <style name="Theme.Shelves" parent="android:Theme"> <item name="android:windowBackground">@drawable/background_shelf</item> <item name="android:windowNoTitle">true</item> </style></resources>
The same exact trick is used inGoogle Maps application that ships with the T-Mobile G1. when the application is launched, the user immediately sees the loading tiles
MapView
. This is only a trick, the theme is simply using a tiled background that looks exactly like the loading tiles
MapView
.
Sometimes the best tricks are also the simplest so the next time you create an activity with an opaque ui or a custom background, remember to change the window's background.
Download the source code of the first example.
Download the source code of shelves.
Posted byromain guyat8: 30
AMLabels: optimization, user
Interface