首页 > 解决方案 > 如何从 Glide 获取图像的宽度和高度

问题描述

我已将<ImageView />高度设置为wrap_content. 我正在添加这个布局recycler adapter

将周围的10 images所有图像都设置为wrap_content

有什么方法可以获取 Glide 渲染的图像的高度。

我正在使用它,但它返回屏幕尺寸而不是图像dimensions

 Glide.with(context).load(imgpth).placeholder(R.drawable.bg_loading)
                            .error(R.drawable.bg_loading).into(imageProdctBanner).getSize(new SizeReadyCallback() {
                        @Override
                        public void onSizeReady(int width, int height) {
                            Log.e("width","wdthheight "+width+"  :  "+height);

                        }
                    });

我需要这个来设置recyclerView的高度。

int totalItems= cardsRecyclerAdapter.getItemCount();

    ViewGroup.LayoutParams params=rvCards.getLayoutParams();
    params.height= heightOfImageView *totalItems;
    rvCards.setLayoutParams(params);

标签: androidperformanceandroid-layoutandroid-fragmentsandroid-recyclerview

解决方案


改用onResourceReady()方法

Glide.with(getContext().getApplicationContext())
     .asBitmap()
     .load(path)
     .into(new SimpleTarget<Bitmap>() {
         @Override
         public void onResourceReady(Bitmap bitmap,
                                     Transition<? super Bitmap> transition) {
             int w = bitmap.getWidth();
             int h = bitmap.getHeight()
             mImageView.setImageBitmap(bitmap);
         }
     });

推荐阅读