自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
*
*/

更多相关文章

  1. Android开发UI之在子线程中更新UI
  2. android 简单线程同步之CountDownLatch
  3. Android 多线程之HandlerThread 详解
  4. 知识梳理:Android子线程中更新UI的3种方法
  5. android 多线程数据库读写分析与优化
  6. Android 开启新线程

随机推荐

  1. Android精品开源项目整理(持续更新中..)
  2. Android程序开发学习笔记系列——基础篇(
  3. 关于Android任意分辨率视频编码的思考与
  4. 对Android 平台下SElinux的理解及遇到过
  5. Android 中的资源访问(Resource)
  6. Android System——事件传递(一) View
  7. Android Canvas 圆角处理
  8. Android Studio 调试
  9. Android通过JDBC直连MySql
  10. Android消息推送机制