概念:
ImagePipeline ——负责从网络、本地图片、Content Provider(内容提供者)或者本地资源那里获取图片,压缩保存在本地存储中和在内存中保存为压缩的图片
Drawee——处理图片的渲染,由3部分组成:
(1)DraweeView——显示图片的View,继承ImageView;大部分时间将使用SimpleDraweeView
(2)DraweeHierarchy——Fresco提供了很多定制功能,如:添加placeholderImage,retryImage,failureImage,backgroundImage;当它们需要显示追踪所有的这些图片并在它们
(3)DraweeController——负责处理图片加载器;如果不想使用ImagePipeline,Fresco允许自定义Image loader
开始使用:
1. 添加 Fresco到 app/build.gradle 中:
dependencies {
compile 'com.facebook.fresco:fresco:0.6.1'
}
2.如果需要从网络中获取图片,在 AndroidManifest.xml中增加Internet权限
:
<uses-permission android:name="android.permission.INTERNET"/>
3.初始化 Fresco. 在使用Fresco的activity中,Fresco 必须在 setContentView()之前初始化
Fresco.initialize(context);
调用Fresco.initialize
一次即可完成初始化,多次调用初始化无意义,所以最好在 Application
里面做这件事。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
在 AndroidManifest.xml
中指定你的 Application 类
<manifest
...
>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:label="@string/app_name"
android:name=".MyApplication"
>
...
</application>
...
</manifest>
4.在xml布局文件中, 加入命名空间:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:placeholderImage="@drawable/img_frame_background" />
</LinearLayout>
Note: If you want to use any Fresco defined properties, you'll need to add a custom namespace definition:
xmlns:fresco="http://schemas.android.com/apk/res-auto"
5.设置图片URI:
Uri imageUri = Uri.parse("https://i.imgur.com/tGbaZCY.jpg");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.sdvImage);
draweeView.setImageURI(imageUri);
/****************************************************************/
在XML中使用Drawees
Drawees 具有极大的可定制性。
下面的例子给出了可以配置的各种选项:
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:fadeDuration="300"
fresco:actualImageScaleType="focusCrop"
fresco:placeholderImage="@color/wait_color"
fresco:placeholderImageScaleType="fitCenter"
fresco:failureImage="@drawable/error"
fresco:failureImageScaleType="centerInside"
fresco:retryImage="@drawable/retrying"
fresco:retryImageScaleType="centerCrop"
fresco:progressBarImage="@drawable/progress_bar"
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
fresco:backgroundImage="@color/blue"
fresco:overlayImage="@drawable/watermark"
fresco:pressedStateOverlayImage="@color/red"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="1dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundWithOverlayColor="@color/corner_color"
fresco:roundingBorderWidth="2dp"
fresco:roundingBorderColor="@color/border_color"
/>
参照:
[1]Fresco文档:http://fresco-cn.org/docs/using-drawees-xml.html