如何在Glide中将动画应用于缓存的图像

时间:2018-12-07 06:53:14

标签: android android-glide

我想使用Glide动画显示图像。我正在从服务器下载此图像。从服务器下载动画时,动画工作正常,但从Glide缓存中获取图像时,动画不工作

requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);

请在下面检查我的代码

RequestOptions requestOptions = new RequestOptions();
requestOptions.timeout((int) NetworkParam.CONNECTION_TIME_OUT);
requestOptions.placeholder(R.drawable.img_blue_shirt);
requestOptions.priority(Priority.HIGH);
requestOptions.signature(new ObjectKey(String.valueOf(imgName)));
//requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);

//requestOptions.error(R.drawable.img_blue_shirt);
try {
    Glide.with(imageView.getContext().getApplicationContext())
         .load(imgUrl)
         .transition(GenericTransitionOptions.with(R.anim.anim_slide_in_left))
         .apply(requestOptions)
         .into(imageView);
} catch (Exception ex) {
    ex.printStackTrace();
}

4 个答案:

答案 0 :(得分:7)

要在glide 4.10中应用图片更改过渡,请使用

Glide.with(this).load(artwork)
                            ..transition(DrawableTransitionOptions.withCrossFade())
                            .into(playerBackground);

答案 1 :(得分:1)

  

默认情况下,Glide在加载图像时不会为其设置动画。如果   图像缓存在磁盘上,Glide会进行动画处理。要更改它,请定义   自定义的TransitionFactory并将其传递给   DrawableTransitionOptions。

有关示例实现,请参见交叉淡入淡出工厂:https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/request/transition/DrawableCrossFadeFactory.java

答案 2 :(得分:0)

是的,我在这里得到了答案,我们无法在缓存的图像上滑动应用动画以获取更多信息,请检查以下链接

https://bumptech.github.io/glide/doc/transitions.html

答案 3 :(得分:0)

根据文档here,在Glide v4中,您可以实现并应用自定义TransitionFactory

对于始终淡入淡出,在Kotlin中,您可以:

  1. 实现TransitionFactory界面
class DrawableAlwaysCrossFadeFactory : TransitionFactory<Drawable> {
  private val resourceTransition: DrawableCrossFadeTransition = DrawableCrossFadeTransition(300, true) //customize to your own needs or apply a builder pattern
  override fun build(dataSource: DataSource?, isFirstResource: Boolean): Transition<Drawable> {
    return resourceTransition
  }
}
  1. 将其应用为过渡
GlideApp.with(this)
  .load(url)
  .transition(DrawableTransitionOptions.with(DrawableAlwaysCrossFadeFactory()))
  .into(image)