1.首先加入依赖
implementation 'com.facebook.fresco:fresco:1.11.0'
implementation 'com.facebook.fresco:animated-gif:1.11.0'
2.配置Application
@Override
public void onCreate() {
super.onCreate();
//Fresco.initialize(this );
Fresco.initialize(this, ImagePipelineConfig.newBuilder(App.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsolutePath())) // 注意Android运行时权限。
.build()
)
.build()
);
}
3.mainactivity
public class MainActivity extends AppCompatActivity {
private String imageUrl="https://img.huxiucdn.com/article/cover/201810/29/153108448766.jpg";
private SimpleDraweeView simpla_view;
private String URL_="https://img3.duitang.com/uploads/item/201605/14/20160514165650_RHr3n.gif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpla_view = findViewById(R.id.simpla_view);
simpla_view.setImageURI("https://img.huxiucdn.com/article/cover/201810/29/153108448766.jpg");
/*Uri uri = Uri.parse("res://com.example.week22/" + R.mipmap.ic_launcher);
simpla_view.setImageURI(uri);*/
//showUrlBlur(simpla_view,imageUrl,100,10);
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
//图片地址
.setUri(imageUrl)
//播放gif 图片
.setAutoPlayAnimations(true)
//点击重新加载时 可以重新加载4 次
.setTapToRetryEnabled(true)
.build();
simpla_view.setController(controller);
}
public void showUrlBlur(SimpleDraweeView draweeView, String url, int iterations, int blurRadius) {
try {
Uri uri = Uri.parse(url);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
.setPostprocessor(new IterativeBoxBlurPostProcessor(iterations, blurRadius))
.build();
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setOldController(draweeView.getController())
.setImageRequest(request)
.build();
draweeView.setController(controller);
} catch (Exception e) {
e.printStackTrace();
}
}
}