2013年2月21日 星期四

[Android] Optimised ImageView

在 4.0 前,使用 ImageView.setImageDrawable(),會執行下列動作:
  1. Set the new Drawable (and keep a reference to it).
  2. Request a layout pass.
  3. Invalidate itself to display the new contents.

step 1 and step 3 是必要的
step 2 是定位 drawable 自 ImageView 中的位置,因此只需要在 drawable 大小改變時使用到。

也就是若 AdapterView 中使用的都是大小相同的 drawable,則能省略 step 2,讓滑動的效能獲得改善


在 4.0 中會先比對大小才呼叫 requestLayout()(run the step 2),4.0 後則能使用 reference 中所提供的 OptimisedImageView。


Relative post
- [Android] Avoid Memory leak


* Reference
- Snippet: ImageView Layout Optimisation | senab