简体   繁体   English

使用滑动加载图像并更改纵横比

[英]Load Image With Glide and Change Aspect Ratio

I have an imageview and I would like to load an image with Glide into it.我有一个图像视图,我想将带有 Glide 的图像加载到其中。 The thing here is, I want all the images have the same height (width match_parent) according to the 16:9 aspect ratio.这里的问题是,我希望所有图像根据 16:9 的纵横比具有相同的高度(宽度 match_parent)。

<ImageView
  android:id="@+id/thumbImage"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:contentDescription="@string/thumbnail_text"
  android:scaleType="fitCenter"
  android:src="@drawable/thumbnail_default"
  android:cropToPadding="true"

  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toTopOf="@id/thumbTitle"/>

Glide code滑行代码

Glide.with(mThumb.getContext())
          .load(getThumbUrl())
          .into(mThumb);

When I load an image with aspect ratio 16:9, everything is great.当我加载宽高比为 16:9 的图像时,一切都很好。 However, when I load another image, the height adjusts to the height of the image.但是,当我加载另一个图像时,高度会调整为图像的高度。

I tried adding Constraint layout dimension ratio我尝试添加约束布局尺寸比

app:layout_constraintDimensionRatio="16:9"

I tried playing around with adjustViewBounds and scaleType but no success.我尝试使用 adjustViewBounds 和 scaleType 但没有成功。

So I think I should play with Glide to adjust the bitmap before loading it to the imageview but I couldn't find a tutorial about that.所以我想我应该在将位图加载到 imageview 之前使用 Glide 来调整它,但我找不到关于它的教程。

How can I show image with width match_parent and height calculated as aspect ratio 16:9?如何显示宽度 match_parent 和高度计算为纵横比 16:9 的图像?

Thank you,谢谢,

Note: I tried注意:我试过了

<ImageView
                android:id="@+id/thumbImage"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:adjustViewBounds="true"
                android:contentDescription="@string/thumbnail_text"
                android:scaleType="centerCrop"
                android:src="@drawable/thumbnail_default"
                android:cropToPadding="true"
                app:layout_constraintDimensionRatio="H,16:9"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@id/thumbTitle"/>

It works, but then if the orientation is changed, the height goes 0.它有效,但如果方向改变,高度变为 0。

One way around is to use a Image view with 16:9 ratio .一种解决方法是使用比例为 16:9 的图像视图。

public class CustomImageView extends AppCompatImageView {
    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height=(width * 9) / 16;
        setMeasuredDimension(width, height);
    }
}

This will make a ImageView with hard code ratio of 16/9 .这将制作一个硬编码比率为16/9ImageView You can use Custom attributes for it to make it more flexible.您可以为其使用自定义属性以使其更加灵活。

Update:- Now with the ConstraintsLayout its easy to break views in ratio .更新:-现在使用ConstraintsLayout可以轻松地按比例打破视图。 U can try following.你可以尝试跟随。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/expandableModes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="16:9"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

Another way is to use a SimpleTarget<> as describe in this post .另一种方法是使用一个SimpleTarget<>如在描述此信息 Then you can resize the image based on the current image's size.然后您可以根据当前图像的大小调整图像大小。

 Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>{ @Override public void onResourceReady(Bitmap resource,GlideAnimation<? extends Bitmap>(){ // resize the bitmap bitmap = resize(width,height); // set the image to the imageView imageView.setImageBitmap(bitmap); } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM