如何让android imageview方块?

时间:2014-01-29 16:43:10

标签: android imageview

我在Android项目中使用ImageView

宽度:match_parent 身高:wrap_content

我将它缩放到fill_XY,但图像不是方形的......我该怎么办?

enter image description here

8 个答案:

答案 0 :(得分:24)

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

答案 1 :(得分:12)

方形图像的最佳方法是使用 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"
        android:scaleType="centerCrop"
        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>

答案 2 :(得分:6)

使用自定义视图并覆盖onMeasure()方法。

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

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

答案 3 :(得分:2)

您可以随时尝试设置自己的尺寸:

android:width="50dp"
android:height="50dp"

你会强制它是那样的方形,但你需要自己找到合适的尺寸。您也可以尝试添加:

android:scaleType="fitXY"

其他信息

实际上将scaleType设置为 fitXY 通常非常糟糕。请查看ImageView documentation并使用最适合您的用例的ScaleType。如果您不确定哪一个最适合您,请尝试全部,看看它们如何影响您的图像。

答案 4 :(得分:2)

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

答案 5 :(得分: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" />

答案 6 :(得分:0)

使用

android:scaleType="fitCenter"

imageView.setScaleType(ScaleType.FIT_CENTER);

答案 7 :(得分:0)

确定您的宽度/高度(例如40dp),&amp;使用centerCrop scaleType

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop" 
/>
相关问题