自pinterest使用了瀑布流展示圖片後,有很多應用開始使用瀑布流的方式,像蘑菇街,美麗說。這裡的瀑布流實現使用了開原始碼。
layout:
<?xml version="1.0" encoding="utf-8"?><com.dodowaterfall.LazyScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/waterfall_scroll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" > <LinearLayout android:id="@+id/waterfall_container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" > </LinearLayout></com.dodowaterfall.LazyScrollView>
整個瀑布流用的是ScrollView的子類LazyScrollView。這個LazyScrollView中設定了一個監聽器介面,用來監聽ScrollView執行的不同階段。介面如下:
public interface OnScrollListener {void onBottom();void onTop();void onScroll();void onAutoScroll(int l, int t, int oldl, int oldt);}
對於每一幅圖,都用一個ImageView的子類FlowView來表示。為了不阻塞UI線程,圖片載入和圖片更新都分別用不同的線程來做。這兩個線程都在FlowView中。FlowView提供了載入和更新的介面給Activity調用。瀑布流執行個體的主Activity是MainActivity,常量都儲存在Constants類中,方便維護。
瀑布流最重要的是圖片的記憶體回收機制,防止發生記憶體溢出的情況(OOM)。
參考:
https://github.com/dodola/android_waterfall
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html