android Glide 支持全局jpg png gif 图片展示

该博客介绍了如何在Android应用中使用Glide库加载常规图片和Gif。通过添加Glide依赖并调用相应的工具类方法,可以实现图片的加载、设置占位符、错误图片以及处理Gif资源。工具类中的loadImage方法处理了常规图片和Gif的显示,并在资源准备好时启动Gif播放。
摘要由CSDN通过智能技术生成

1.依赖

implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

2.工具类

    /**
     * 加载 常规和gif 通用
     *
     * @param context
     * @param imageView
     * @param url        图片链接
     * @param placeResId 默认占位图片
     * @param errorResId 错误图片
     */
    public static void loadImage(Context context, ImageView imageView, String url, int placeResId, int errorResId) {
        /**常规和gif 通用*/
        Glide.with(context).asDrawable().load(url).placeholder(placeResId).error(errorResId).into(new CustomTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                /**GifDrawable (gif图片类型)*/
                if (resource instanceof GifDrawable) {
                    GifDrawable gifDrawable = (GifDrawable) resource;
                    imageView.setImageDrawable(gifDrawable);
                    gifDrawable.start();
                } else {
                    /**常规图片*/
                    imageView.setImageDrawable(resource);
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                /**加载错误图片*/
                imageView.setImageDrawable(errorDrawable);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
                /**占位图片*/
                imageView.setImageDrawable(placeholder);
            }
        });
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值