在 ImageView 中使用 Glide 调整图像大小 Android

Resize images with Glide in a ImageView Android

我对android中图片的处理有很多疑惑,希望你能解决。

此时我有一张图像,高 320 dp,宽度 match_parent,大约占屏幕的 60%。 这张用Glide加载的图,我个人有的1080的一些图

我尝试制作 centroCrop 和 fitXY,但总是使图像变形。我知道的第一种会裁剪图像,但第二种适合一个尺寸,但它确实会使图像变高或变宽。

有没有什么办法,用Glide插入它并按原样查看? 我需要触及 ImageView 的哪些属性以及 Glide 的哪些属性?

<ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        app:layout_collapseMode="parallax"
        android:scaleType="fitXY"
        app:layout_scrollFlags="scroll|enterAlways" />
Glide
    .with(context)
    .load(path)     
    .apply(new RequestOptions().override(600, 200))
    .centerCrop() 
    .into(imageViewResizeCenterCrop);

@Raghunandan 是 right.you 应该尝试以自己的方式进行转型。

如果你想在不使用glide的情况下加载图像,那么你可以使用scaleType "fitCenter"或"centerInside",如下所示-

<ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        app:layout_collapseMode="parallax"
        android:scaleType="centerInside"
        app:layout_scrollFlags="scroll|enterAlways" />

对于 Glide,您可以使用 override(w,h)。当你使用 override(w,h) 时,glide 会生成一个新的位图,其宽度和高度在 override(w,h) 中提到,然后将图像加载到 ImageView 中。您可以使用 fitCenter() 对齐图像。您还可以使用 diskCacheStrategy()。如果你不使用它,Glide 将只捕获新生成的位图。如果你还想捕获原始图像,请使用 diskCacheStrategy(DiskCacheStrategy.ALL).

Glide.with(context)
            .load(image_path)
            .override(800, 400)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView)

希望对您有所帮助。

Glide 4.x 的最新版本中,

Override 必须通过 RequestOptions 访问。您可以通过 apply()

添加 RequestOptions
Glide.with(context)
    .load(path)
    .apply(new RequestOptions().override(600, 200))
    .into(imageViewResizeCenterCrop);

您必须对图像视图执行以下操作:

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="fitCenter" />

还有你的滑行:

    Glide.with(getBaseContext())
            .load(path)
            //.placeholder(R.drawable.drawable)
            .error(R.drawable.noimg)
            .animate(R.anim.animation_fade_in)
            .into(mImageView);

这很适合我。

在滑动 4.x 'override' 在 'apply' :

Glide.with(getBaseContext())
 .load(path)
 .apply(RequestOptions.placeholderOf(R.mipmap.no_wifi)
 .error(R.mipmap.no_wifi)
 .override(500,500))
 .into(mImageView);

Glide 4.x - 如果你想获取位图,试试:

Glide.with(mContext).asBitmap().
                        load(pictureUri)
                        .apply(new RequestOptions().override(50, 50))
                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                return false;
                            }
                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            // resource is your loaded Bitmap
                            imgView.setImageBitmap(resource);
                            return true;
                        }
                    }).submit();