如何使Android中的ImageView变成正方形?

24

我在Android项目中使用了ImageView

宽度:match_parent 高度:wrap_content

然后我将其缩放为fill_XY,但是图像仍不是正方形...我该怎么办?

输入图像描述


使用固定的宽度和高度来设置 ImageView。 - rajahsekar
这里有另一篇文章可能会有所帮助。看一下 https://dev59.com/8WQn5IYBdhLWcg3w36TV - Thomas R.
9个回答

65

使用ConstraintLayoutconstraintDimensionRatio是制作正方形图片的最佳方式,而无需指定固定的高度/宽度。

  <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>

3
android:scaleType="centerCrop" 真的是一个糟糕的想法,会损坏图片。 - narcis dpr
这个很好用,当然没有使用centerCrop。 - timeSmith

27

通过扩展 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" />

7

使用自定义视图并重写onMeasure()方法。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    setMeasuredDimension(width, width);
}

6
创建自己的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);
    }
}

3
你可以尝试设置自己的尺寸:
android:width="50dp"
android:height="50dp"

用这种方法可以强制它成为精确的正方形,但你需要自己找到正确的尺寸。你也可以尝试添加以下内容:

android:scaleType="fitXY"

额外信息

实际上将scaleType设置为fitXY通常是非常糟糕的。请查看ImageView文档,并使用最适合你用例的ScaleType。如果不确定哪个最适合你,请尝试它们所有并观察它们如何影响你的图像。


2

可以这样做:

<?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" />


你的回答与之前发布的“被接受的答案”有何不同? - Amir Dora.
@AmirDora,“已接受的答案”对我没有用,但我发现我需要更改父视图的高度:android:layout_height="wrap_content" - B-GangsteR
你可以在原回答下留言:)。无论如何都没有问题。 - Amir Dora.

0

我想对已接受的答案进行小小的补充。在我的情况下,目标是:

  1. 通过填充ConstraintLayout来显示一个正方形图像
  2. 具有W > H的图像,上下有填充
  3. 具有H > W的图像,左右有填充 为了实现这一点,缺少了这行代码:
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>


-1
确定您的宽度/高度(例如40dp),并使用centerCrop scaleType
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop" 
/>

-1

使用

android:scaleType="fitCenter"

或者

imageView.setScaleType(ScaleType.FIT_CENTER);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接