Android Glide 获取动图的第一帧

一、说明

Glide 可以加载 2 种动图,一种是 Gif 图,另一种是 Webp 动图。

有时候我们需要获取动图的第一帧,并以封面的形式显示,那该怎样获取呢?

二、获取 Webp 第一帧

我这儿的 Webp 显示用到了一个三方库:

"com.github.zjupure:webpdecoder:2.3.$glideVersion"

获取第一帧:

Glide.with(context).asDrawable().load(url)
        .optionalTransform(WebpDrawable.class, new WebpDrawableTransformation(new CenterCrop()))
        .into(new CustomTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                if (resource instanceof WebpDrawable) {
                    // 从 webp 图片中获取第一帧
                    WebpDrawable webpDrawable = (WebpDrawable) resource;
                    Bitmap bitmap = webpDrawable.getFirstFrame();
                }
            }
            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {}
        });

三、获取 Gif 第一帧

场景 1:直接显示到 ImageView

Glide.with(context)
        .setDefaultRequestOptions(new RequestOptions().frame(1))
        .load(url)
        .into(imageView);

可以通过 setDefaultRequestOptions(new RequestOptions().frame(1)) 来设置获取第一帧。

场景 2:获取第一帧 bitmap

Glide.with(context)
        .asBitmap()
        .load(url)
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                // resource 即为第一帧的图片
            }
            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });

请求 Gif 的时候我们用 bitmap 接收,Glide 发现目标类型无法承载整个 Gif 资源,就会拿第一帧给你,这样你也就获取到了第一帧。

相关推荐

  1. Android Glide 获取第一

    2024-04-30 20:02:01       11 阅读
  2. 获取视频第一,以及后续上传

    2024-04-30 20:02:01       31 阅读
  3. maya获取长度

    2024-04-30 20:02:01       19 阅读
  4. 分享: 网站

    2024-04-30 20:02:01       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-30 20:02:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-30 20:02:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-30 20:02:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-30 20:02:01       18 阅读

热门阅读

  1. 原生小程序分页/上拉加载(通过页面生命周期)

    2024-04-30 20:02:01       10 阅读
  2. Czi.算法学习(三)

    2024-04-30 20:02:01       13 阅读
  3. 表达式解析器MVEL的了解

    2024-04-30 20:02:01       9 阅读
  4. MSP未来趋势

    2024-04-30 20:02:01       9 阅读
  5. 【Spring AI】前言

    2024-04-30 20:02:01       12 阅读
  6. 【多维动态规划】Leetcode 72. 编辑距离【中等】

    2024-04-30 20:02:01       8 阅读
  7. Redis使用手册之字符串

    2024-04-30 20:02:01       15 阅读
  8. AtCoder Beginner Contest 351 A题 The bottom of the ninth

    2024-04-30 20:02:01       10 阅读