Glide 加载Gif 并且自适应图片大小

2022-04-21  本文已影响0人  皓皓amous
Glide.with(context)
                  .asBitmap()
                  .load(uri)
                  .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                      @Override
                      public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                          int width = resource.getWidth();
                          int height = resource.getHeight();
                          // 设置想要的大小
                          int newWidth = ScreenUtils.getScreenWidth(context);
                          int newHeight = newWidth * height / width;
                          // 计算缩放比例
                          float scaleWidth = ((float) newWidth) / width;
                          float scaleHeight = ((float) newHeight) / height;
                          // 取得想要缩放的matrix参数
                          Matrix matrix = new Matrix();
                          matrix.postScale(scaleWidth, scaleHeight);
                          // 得到新的图片
                          resource = Bitmap.createBitmap(resource, 0, 0, width, height, matrix, true);
                          gifImageView.setImageBitmap(resource);
                      }
                  });

上一篇 下一篇

猜你喜欢

热点阅读