依赖
// 一般依赖:
implementation 'com.facebook.fresco:fresco:1.11.0'
// 如果需要支持gif,再添加:
implementation 'com.facebook.fresco:animated-gif:0.12.0'
初始化
·建议在App启动就初始化,所以建议写在Application#onCreate()中,记得在manifest.xml注册Application。
一般初始化:
·public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
...
高级初始化-配置缓存文件夹
// 高级初始化:
Fresco.initialize(this, ImagePipelineConfig.newBuilder(App.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
.setBaseDirectoryPath(new File("SD卡路径")) // 注意Android运行时权限。
.build()
)
.build()
);
<?xml version="1.0" encoding="utf-8"?>
xml文件这个要配置
xmlns:fresco="http://schemas.android.com/apk/res-auto"
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="500dp"
android:layout_height="500dp"
android:id="@+id/sim_fresco"
fresco:fadeDuration="1000"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="40dp"
fresco:roundingBorderWidth="2dp"
fresco:roundingBorderColor="@color/colorAccent"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
/>
</LinearLayout>
main中的使用
public class MainActivity extends AppCompatActivity {
private SimpleDraweeView sim_fresco;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
sim_fresco = (SimpleDraweeView) findViewById(R.id.sim_fresco);
Uri uri = Uri.parse("http://p5.qhimg.com/t01145b0a8faee89a4a.jpg");
//sim_fresco.setImageURI(uri);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true)
.build();
sim_fresco.setController(controller);
}
}
drawee支持如下属性:
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"
//图片加载失败时显示,提示用户点击重新加载,重复加载4次还是没有加载出来的时候才会显示failureImage的图片
fresco:retryImageScaleType="centerCrop" //重新加载的图片缩放类型
fresco:progressBarImage="@drawable/progress_bar" //进度条图片
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
//进度图自动旋转间隔时间(单位:毫秒ms)
fresco:backgroundImage="@color/blue"
//背景图片,这里的背景图片首先被绘制
fresco:overlayImage="@drawable/watermark"
// 设置叠加图,在xml中只能设置一张叠加图片,如果需要多张图片的话,需要在java代码中设置哦
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" //边框颜色