[英]Load Image With Glide and Change Aspect Ratio
我有一個圖像視圖,我想將帶有 Glide 的圖像加載到其中。 這里的問題是,我希望所有圖像根據 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.with(mThumb.getContext())
.load(getThumbUrl())
.into(mThumb);
當我加載寬高比為 16:9 的圖像時,一切都很好。 但是,當我加載另一個圖像時,高度會調整為圖像的高度。
我嘗試添加約束布局尺寸比
app:layout_constraintDimensionRatio="16:9"
我嘗試使用 adjustViewBounds 和 scaleType 但沒有成功。
所以我想我應該在將位圖加載到 imageview 之前使用 Glide 來調整它,但我找不到關於它的教程。
如何顯示寬度 match_parent 和高度計算為縱橫比 16:9 的圖像?
謝謝,
注意:我試過了
<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"/>
它有效,但如果方向改變,高度變為 0。
一種解決方法是使用比例為 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);
}
}
這將制作一個硬編碼比率為16/9
的ImageView
。 您可以為其使用自定義屬性以使其更加靈活。
更新:-現在使用ConstraintsLayout
可以輕松地按比例打破視圖。 你可以嘗試跟隨。
<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>
另一種方法是使用一個SimpleTarget<>
如在描述此信息。 然后您可以根據當前圖像的大小調整圖像大小。
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.