可以使用TransitionDrawable来解决这个问题。TransitionDrawable是一个Drawable对象,它可以将两个Drawable对象之间平滑地过渡。可以在加载新图片之前,使用一个TransitionDrawable将ImageView中原有的图像和一个空的占位符Drawable对象进行淡出操作,然后再使用GLIDE 4来加载新图像并将其绑定到ImageView上。这样可以避免出现闪烁的问题。以下是示例代码:
ImageView imageView = findViewById(R.id.image_view);
Drawable placeholder = ContextCompat.getDrawable(this, R.drawable.placeholder);
Drawable[] layers = new Drawable[]{
imageView.getDrawable(),
new ColorDrawable(Color.TRANSPARENT)
};
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(200);
GlideApp.with(this)
.load(imageUrl)
.placeholder(placeholder)
.centerCrop()
.into(new ImageViewTarget(imageView) {
@Override
protected void setResource(@Nullable Drawable resource) {
imageView.setImageDrawable(resource);
transitionDrawable.reverseTransition(200);
}
});