滑动加载图像并基于图像宽度更改图像视图宽度

时间:2018-08-18 16:44:41

标签: java android image android-glide

我正在使用Glide库将图像加载到回收视图中,当图像较大时,一切都很好,但是当图像尺寸较小时,我想减小图像视图宽度以避免低质量的视图,我尝试了以下代码行,但并非全部时间并重新加载图像时更改所有图像大小:

 Glide.with(context).load(url).listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {


                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

                        ((imageHolder) holder).pb.setVisibility(View.INVISIBLE);
                        final GifDrawable gifDrawable;
                        if (resource instanceof Animatable) {
                            gifDrawable = (GifDrawable) resource;
                            gifDrawable.start();
                        } else {
                            gifDrawable = null;
                        }
                        int imageViewWidth = RelativeLayout.LayoutParams.MATCH_PARENT;
                        if (getScreenWidth() > (2.5 * resource.getIntrinsicWidth())) {
                            imageViewWidth = getScreenWidth() / 2;

                        } else {
                        }
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageViewWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
                        ((imageHolder) holder).imageView.setLayoutParams(lp);
                        return false;
                    }
                }).apply(new RequestOptions().placeholder(R.drawable.s).centerInside()).into(((imageHolder) holder).imageView);

,这也是我的xml:

 <ImageView
    android:id="@+id/p_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"

    />

我不得不说,如果不下载图像,我将无法获得图像尺寸。

1 个答案:

答案 0 :(得分:0)

您尝试过吗 .centerCrop()?

更改centerInside与centerCrop