自pinterest使用了瀑布流展示圖片後,有很多應用開始使用瀑布流的方式,像蘑菇街,美麗說。這裏的瀑布流實現使用了開源代碼。
layout:
<?xml version="1.0" encoding="utf-8"?> <com.dodowaterfall.LazyScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/waterfall_scroll" 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
/**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階羣:278401545
*
*/