Fresco是最全,最好,但是体积也比较大的图片处理的开源框架,能够从网络,内存,缓存中加载图片,节省了流量和CPU资源,实现三级缓存,底层使用的C,所以对图片的内存管理非常高效.
使用Fresco的步骤:
1. 进行关联,在build.gradle里dependencies添加 compile 'com.facebook.fresco:fresco:0.12.0' 即可
提示:Fresco可以实现非常多的功能,所以如果有其他需求,还要在build.gradle里dependencies添加对应关联
// 在 API < 14 上的机器支持 WebP 时,需要添加
compile 'com.facebook.fresco:animated-base-support:0.12.0'
// 支持 GIF 动图,需要添加
compile 'com.facebook.fresco:animated-gif:0.12.0'
// 支持 WebP (静态图+动图),需要添加
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'
// 仅支持 WebP 静态图,需要添加
compile 'com.facebook.fresco:webpsupport:0.12.0'
2. 初始化Fresco类,在自定义的Application中,添加: Fresco.initialize(this);
提示:在清单文件里配置你的Application.
3. 在清单文件里配置网络权限.
4.在XML布局文件中,加入命名控件(在你使用Fresco控件的布局xml文件里)<uses-permission android:name="android.permission.INTERNET"/>
<!-- 其他元素-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
5.加入SimpleDraweeView:
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:placeholderImage="@drawable/my_drawable"
/>
6. 代码中加载图片:
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);