关于安卓使用glide加载得出drawable,bitmap

背景

recyclerview中,IamgeView的高度根据glide加载图片返回的宽高进行配置,会偶发,glide加载生成的drawable宽高混乱,代码如下:

            Glide.with(context.getApplicationContext())
                    .load(img == null ? t : img)
                    .apply(options)
                    .addListener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            if (listener != null) {
                                listener.onLoadFailed(e, model, target, isFirstResource);
                            }
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            if (listener != null) {
                                listener.onResourceReady(resource, model, target, dataSource, isFirstResource);
                            }
                            return false;
                        }
                    })
                    .into(imageView);

实际开发中,发现onResourceReady的resource,宽高会偶发错乱,因此,换一种方法实现,内容如下:

 Glide.with(context.getApplicationContext())
                    .asBitmap()
                    .load(img == null ? t : img)
                    .apply(options)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            if (listener != null) {
                                int width = resource.getWidth();
                                int height = resource.getHeight();
                                LogUtils.d("onResourceReady url: " + finalImg + " width: " + width + " height: " + height);
                                listener.ready((width >= height), width, height, resource);
                            }
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                            if (listener != null) {
                                listener.loadFailed();
                            }
                        }
                    });

这种情况下,bitmap经过实测,它的宽高是准确的,所以目前换成了这种方法实现。

原因是因为recyclerview中,imageview的复用问题,导致了drawable的问题,最后加载完图片也是有问题的。该问题需要结合复用,与ImageView进行思路。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android使用 Glide 加载网络视频,你可以按照以下步骤进行操作: 1. 首先,在你的 Android 项目中添加 Glide 的依赖。你可以在项目的 `build.gradle` 文件中的 `dependencies` 块中添加以下代码: ```groovy implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' ``` 2. 确保你已经在 AndroidManifest.xml 文件中添加了网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在你需要加载网络视频的地方,使用 Glide 的 `VideoViewTarget` 类来加载视频。首先,导入 Glide 相关的类: ```java import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.ViewTarget; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.request.transition.TransitionFactory; ``` 4. 然后,使用以下代码加载视频: ```java String videoUrl = "Your video URL"; Glide.with(context) .load(videoUrl) .apply(RequestOptions.noTransformation()) .into(new ViewTarget<View, Drawable>(yourVideoView) { @Override public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) { if (resource instanceof GifDrawable) { GifDrawable gifDrawable = (GifDrawable) resource; gifDrawable.setLoopCount(GifDrawable.LOOP_FOREVER); gifDrawable.start(); } else if (resource instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) resource; Bitmap bitmap = bitmapDrawable.getBitmap(); // Do something with the bitmap } } }); ``` 在上面的代码中,将 "Your video URL" 替换为你要加载的网络视频的 URL,同时将 `yourVideoView` 替换为要显示视频的 `VideoView` 或 `SurfaceView`。 这样,使用 Glide 加载网络视频就完成了。请注意,Glide 也可以加载其他类型的图片资源和动画资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值