简体   繁体   English

在Android上使用Fresco显示Gif预览(例如,作为第一帧)

[英]Show Gif preview with Fresco on Android (as first frame, for instance)

I use Fresco library to show animated gif files in RecyclerView and android. 我使用壁画库在RecyclerView和android中显示动画gif文件。 My code is pretty simple, I do this in onBindViewHolder(): 我的代码非常简单,我在onBindViewHolder()中执行此操作:

 DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(url)
                .setAutoPlayAnimations(true)
                .build();
 simpleDraweeView.setController(controller);

Everything is fine, but it looks like GIF images are not cached and reloaded when I scroll up or down. 一切都很好,但是当我向上或向下滚动时,看起来GIF图像并未被缓存和重新加载。 So I decided to show preview for GIF files just like simple images. 因此,我决定像简单的图像一样显示GIF文件的预览。 Seems to setLocalThumbnailPreviewsEnabled(true) for ImageRequest doesn't work for GIF images. 似乎为ImageRequest设置setLocalThumbnailPreviewsEnabled(true)对GIF图像不起作用。 Then I tried to use ImageDecodeOptionsBuilder with setForceStaticImage(true) - it displays GIFs as static images (almost what I need, but I need to start animation after static image is loaded). 然后,我尝试将ImageDecodeOptionsBuilder与setForceStaticImage(true)一起使用-它会将GIF显示为静态图像(几乎是我所需要的,但是我需要在加载静态图像后启动动画)。 Here is the all my code: 这是我所有的代码:

    Uri imageUri = Uri.parse(url);
    ImageDecodeOptionsBuilder decodeOptionsBuilder = new ImageDecodeOptionsBuilder();
    decodeOptionsBuilder.setForceStaticImage(true);
    decodeOptionsBuilder.setDecodePreviewFrame(true);
    ImageDecodeOptions imageDecodeOptions = new ImageDecodeOptions(decodeOptionsBuilder);
    ImageRequest request =  ImageRequestBuilder.newBuilderWithSource(imageUri)
            .setImageDecodeOptions(imageDecodeOptions)
            .setLocalThumbnailPreviewsEnabled(true)
            .build();

    DraweeController controller = Fresco.newDraweeControllerBuilder()
            .setImageRequest(request)
            .setAutoPlayAnimations(true)
            .build();
    simpleDraweeView.setController(controller);

I also tried to play with setAutoPlayAnimations(true/false) and other parameters (like decodeOptionsBuilder.setDecodePreviewFrame(true)) but still without success. 我也尝试过使用setAutoPlayAnimations(true / false)和其他参数(例如decodeOptionsBuilder.setDecodePreviewFrame(true)),但仍然没有成功。

So my question is: how I can show the static images as preview images (like placeholders) for GIFs? 所以我的问题是:如何将静态图像显示为GIF的预览图像(如占位符)? If It's first(or some other) frame from GIF it would be cool. 如果它是GIF的第一帧(或其他帧),那就太酷了。 Any help is appreciated. 任何帮助表示赞赏。

The easiest way to do this is as you described: 最简单的方法如您所述:

Set the GIF where you force the static image. 在强制静态图像的位置设置GIF。 Once you want to play the animation (eg on tap), set the image again but this time with setAutoPlayAnimations . 一旦要播放动画(例如点击),再次设置图像,但这一次使用setAutoPlayAnimations

As an alternative, you can also not start the animation automatically but start it on tap, see https://github.com/facebook/fresco/blob/master/samples/animation/src/main/java/com/facebook/samples/animation/MainActivity.java#L131 或者,您也不能自动启动动画,而是在点击时启动它,请参见https://github.com/facebook/fresco/blob/master/samples/animation/src/main/java/com/facebook/samples /animation/MainActivity.java#L131

Basically, you build your controller like this: 基本上,您可以这样构建控制器:

DraweeController gifController = Fresco.newDraweeControllerBuilder()
    .setUri(animatedGifUri)
    .setControllerListener(new BaseControllerListener<ImageInfo>() {
      @Override
      public void onFinalImageSet(
          String id,
          ImageInfo imageInfo,
          Animatable anim) {
        if (anim != null) {
          // start the animation with anim.start() whenever you want
        }
      }
    })
    .build();

Unfortunately, there is currently no better way to do this. 不幸的是,目前没有更好的方法可以做到这一点。 However, we're looking into improving the animation API for Fresco in the future. 但是,我们将来会考虑改进Fresco的动画API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM