使用滑动加载图像并更改纵横比
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 的图像时,一切都很好。但是,当我加载另一张图片时,高度会调整为图片的高度。
我尝试添加 Constraint layout dimension ratio
app:layout_constraintDimensionRatio="16:9"
我尝试使用 adjustViewBounds 和 scaleType 但没有成功。
所以我认为我应该先使用 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);
}
}
这将使 ImageView
的硬编码比率为 16/9
。您可以为其使用自定义属性,使其更加灵活。
更新:- 现在 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<>
,如 this post 中所述。
然后您可以根据当前图像的大小调整图像的大小。
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);
}
})
我有一个图像视图,我想用 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 的图像时,一切都很好。但是,当我加载另一张图片时,高度会调整为图片的高度。
我尝试添加 Constraint layout dimension ratio
app:layout_constraintDimensionRatio="16:9"
我尝试使用 adjustViewBounds 和 scaleType 但没有成功。
所以我认为我应该先使用 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);
}
}
这将使 ImageView
的硬编码比率为 16/9
。您可以为其使用自定义属性,使其更加灵活。
更新:- 现在 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<>
,如 this post 中所述。
然后您可以根据当前图像的大小调整图像的大小。
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); } })