2012年4月1日 星期日

[AndroidDev] Window Backgrounds & UI Speed

In this article, you will discover how to speed up the drawing and the perceived startup time of your activities. Both these techniques rely on a single feature, the window's background drawable.

  • When you setup your user interface by calling setContentView() on an Activity.
  • 該 activity's window 會被加上你所設的 view 以及 Android 為你所建立的 views。(其中最重要的是 DecorView.)
    • Android adds your views to the Activity'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 is the DecorView.)
  • The DecorView is the view that actually holds the window's background drawable.
  • 藉由呼叫 getWindow().setBackgroundDrawable() 便可以改變 activity's window background 也就是 DecorView's background drawable.
  • 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 a ColorDrawable.
  • 但此預設背景其實是會影響效能的!
  • An easy way to make such an application draw faster is to remove the background drawable. Since the user interface is entirely opaque, drawing the background is simply wasteful. Removing the background improves the performance quite nicely.
  • Removing the window's background can be achieved very easily by using a custom theme. To do so, first create a file calledres/values/theme.xml containing the following:
    
        
    
    
  • This trick comes in very handy for any app that uses a MapView, a WebView or any other full screen opaque view.

  • Using a theme to change the window's background is also a fantastic way to improve the perceived startup performance of some of your activities. This particular trick can only be applied to activities that use a custom background, like a texture or a logo.
  • 如果是在 XML layout 或是 onCreate() 中設定背景,那麼 user 開啟 activity 時會先看到 default theme and its dark background. 等到 content view and first layout/drawing 被 inflate 才會顯示你所設定的背景。
    • If this application simply set the wooden background in the XML layout or in onCreate() the user would see the application startup with the default theme and its dark background. The wooden texture would only appear after the inflation of the content view and the first layout/drawing pass.
  • 改善方式:
    • 將背景設定在 activity theme 中,這樣系統在 application 開始時會已最快的速度載入該 theme,所以 user 再也不會先看到 default background。
      • 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.
    • ex: The same exact trick is used in the Google Maps application that ships with the T-Mobile G1. When the application is launched, the user immediately sees the loading tiles of MapView. This is only a trick, the theme is simply using a tiled background that looks exactly like the loading tiles of MapView.


* Reference
- Android Developers Blog: Window Backgrounds & UI Speed

沒有留言:

張貼留言