Glide加载转换Bitmap

今天项目里面用到了需要加载图片转换成Bitmap位图,本来我在项目里面就使用Glide框架进行图片的加载,所以就使用了Glide这种方法进行转换Bitmap。

方法1

   Glide.with(getActivity()).load(mUserEntity.getData().getCover()).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                mBitmapCover = resource;
            }
        });

方法2

Glide.with(getActivity()).load(mUserEntity.getData().getCover()).into(new Target<GlideDrawable>() {
            @Override
            public void onLoadStarted(Drawable placeholder) {
            //Drawable进行转换
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

            }

            @Override
            public void onLoadCleared(Drawable placeholder) {

            }

            @Override
            public void getSize(SizeReadyCallback cb) {

            }

            @Override
            public void setRequest(Request request) {

            }

            @Override
            public Request getRequest() {
                return null;
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onDestroy() {

            }
        });
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Glide加载 Bitmap 并设置格式,可以使用 `BitmapTransformation` 类或者 `DecodeFormat` 类。 1. 使用 `BitmapTransformation` 类 在 Glide 中,可以使用 `BitmapTransformation` 类来对加载Bitmap 进行转换。可以通过继承 `BitmapTransformation` 类,并实现 `transform()` 方法来对 Bitmap 进行转换。在 `transform()` 方法中,可以使用 `Bitmap` 的 `setConfig()` 方法来设置 Bitmap 的格式。 以下是一个设置 Bitmap 格式的示例: ```java public class BitmapFormatTransformation extends BitmapTransformation { public BitmapFormatTransformation(Context context) { super(context); } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { // 设置 Bitmap 格式为 RGB_565 toTransform.setConfig(Bitmap.Config.RGB_565); return toTransform; } @Override public String getId() { return "BitmapFormatTransformation"; } } ``` 使用时,可以通过 `transform()` 方法将 `BitmapFormatTransformation` 对象传递给 Glide 的 `transform()` 方法: ```java Glide.with(context) .load(imageUrl) .transform(new BitmapFormatTransformation(context)) .into(imageView); ``` 2. 使用 `DecodeFormat` 类 除了使用 `BitmapTransformation` 类之外,还可以使用 `DecodeFormat` 类来设置 Bitmap 的格式。可以通过 `DecodeFormat` 类的 `PREFER_RGB_565` 常量来设置 Bitmap 格式为 RGB_565。 以下是一个设置 Bitmap 格式的示例: ```java Glide.with(context) .asBitmap() .format(DecodeFormat.PREFER_RGB_565) .load(imageUrl) .into(imageView); ``` 这里使用 `asBitmap()` 方法将图片加载Bitmap,并通过 `format()` 方法设置 Bitmap 格式为 RGB_565。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值