问题描述
您好,我正在尝试将图像调整为卡片视图的大小,但有些图像适合,而其中一些不希望所有图像都适合卡片视图
我已经使用了所有比例类型,但我发现最好的是中心裁剪,但在使用后仍然有些图像不适合
这里是 XML 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_marginStart="1dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="1dp"
app:cardCornerRadius="13dp"
app:cardElevation="1dp"
app:cardMaxElevation="4dp">
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="@string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="@style/RoundedCorner" />
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
解决方法
内部卡片视图使用:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="@style/RoundedCorner" />
,
我认为没有内置的方法可以实现这一点。
还有“适合”建议您的目标 ImageView
和您要在其上显示的图像具有相同的宽高比。否则,虽然您可以“适应”它,但图像将在一维上被压扁或拉伸。
确认后,您可以使用众多图像处理库之一来执行此操作。
流行的是
Glide 和 Picasso 的 API 非常相似,因此如果您选择了其中一个,然后又想更改为另一个,这很容易。
查看 FitCenter transformation 以了解 Glide。我认为这就是您要找的东西,但是请记住,如果您的宽度与高度的比例不匹配,图像会看起来很奇怪。
如果你想要毕加索,check this answer。