我在Android项目中使用了ImageView
。
宽度:match_parent
高度:wrap_content
然后我将其缩放为fill_XY
,但是图像仍不是正方形...我该怎么办?
使用ConstraintLayout和constraintDimensionRatio是制作正方形图片的最佳方式,而无需指定固定的高度/宽度。
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
通过扩展 ImageView 来创建自己的 ImageView。我是这样做的:
Icon.java 文件
public class Icon extends ImageView {
public Icon(final Context context) {
super(context);
}
public Icon(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public Icon(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int width, int height) {
super.onMeasure(width, height);
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
if (measuredWidth > measuredHeight) {
setMeasuredDimension(measuredHeight, measuredHeight);
} else {
setMeasuredDimension(measuredWidth, measuredWidth);
}
}
}
XML文件
<com.mypackage.Icon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/screen" />
使用自定义视图并重写onMeasure()方法。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
imageView
。
下面是一个例子。public class SquareImageView extends AppCompatImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size;
if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY ^ MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY)
size = width;
else
size = height;
}
else
size = Math.min(width, height);
setMeasuredDimension(size, size);
}
}
android:width="50dp"
android:height="50dp"
用这种方法可以强制它成为精确的正方形,但你需要自己找到正确的尺寸。你也可以尝试添加以下内容:
android:scaleType="fitXY"
额外信息
实际上将scaleType设置为fitXY通常是非常糟糕的。请查看ImageView文档,并使用最适合你用例的ScaleType。如果不确定哪个最适合你,请尝试它们所有并观察它们如何影响你的图像。
可以这样做:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="wrap_content"
。 - B-GangsteR我想对已接受的答案进行小小的补充。在我的情况下,目标是:
app:layout_constraintBottom_toBottomOf="parent"
<androidx.cardview.widget.CardView
android:id="@+id/preview_img_cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<ImageView
android:id="@+id/attached_image_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="fitCenter"
android:src="@drawable/placeholder_horizontal"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
centerCrop
scaleType
。<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
/>
使用
android:scaleType="fitCenter"
或者
imageView.setScaleType(ScaleType.FIT_CENTER);