fresco图片加载框架
程序员文章站
2024-03-17 09:36:10
...
1.首先需要导入两个依赖:
implementation ‘com.facebook.fresco:fresco:1.11.0’
implementation ‘com.facebook.fresco:animated-gif:1.10.0’
2.普通的加载图片
XML文件
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
//首先需要有一个app类并且在清单注册,这步的意思是初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
//初始化之后就可以直接使用
Uri uri=Uri.parse("https://img02.sogoucdn.com/app/a/100520024/ad4741145c4dfd2c0f7c8afed8b1e029");
img.setImageURI(uri);
3.加载GIF图片并且渐变,从模糊到清晰
XML文件
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/img"
app:fadeDuration="5000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
//设置GIF动图
BaseControllerListener<ImageInfo> baseControllerListener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
animatable.start();
}
@Override
public void onFailure(String id, Throwable throwable) {
Toast.makeText(MainActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
}
};
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true) //播放gif 图片
.setControllerListener(baseControllerListener)
.build();
imgs.setController(controller);