如何使android imageview方形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21437224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:34:39  来源:igfitidea点击:

How do I make android imageview square?

androidimageview

提问by Gabriel Molter

I'm using an ImageViewin an Android project.

ImageView在 Android 项目中使用 。

Width: match_parentHeight: wrap_content

宽度:match_parent高度:wrap_content

the I scale it to fill_XY, but the image is not square yet... what can I do?

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

enter image description here

在此处输入图片说明

采纳答案by Guy

You can always try setting your own dimensions:

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

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

You'll force it to be exact square that way but you'll need to find the right dimensions yourself. You could also try adding:

您将强制它以这种方式精确平方,但您需要自己找到正确的尺寸。您也可以尝试添加:

android:scaleType="fitXY"

Additional info

附加信息

Actually setting scaleType to fitXYis usually very bad. Please check out ImageView documentationand use the ScaleType that is most suitable for your use case. If you're not sure which one is best for you, try them all and see how they affect your Image.

实际上将 scaleType 设置为fitXY通常非常糟糕。请查看ImageView 文档并使用最适合您的用例的 ScaleType。如果您不确定哪一个最适合您,请尝试所有这些,看看它们如何影响您的形象。

回答by PriyankVadariya

The best way for square image is use ConstraintLayoutwith constraintDimensionRatioWithout give fix height/width.

方形图像的最佳方法是使用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>

回答by Illegal Argument

Try creating your own imageview by extending ImageView. This is how I do it:

尝试通过扩展 ImageView 创建您自己的 imageview。这就是我的做法:

Icon.java file

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 file

xml文件

<com.mypackage.Icon
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/screen" />

回答by Thomas R.

Use a custom view and override the onMeasure() method.

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

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

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

回答by Sourav Bagchi

Create your own imageView. Here is one example

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

回答by B-GangsteR

It could be done like this:

可以这样做:

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

回答by AbdelHady

Determine your width/height (for example 40dp), & use a centerCropscaleType

确定您的宽度/高度(例如 40dp),并使用 centerCropscaleType

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop" 
/>

回答by ElDuderino

Use

android:scaleType="fitCenter"

or

或者

imageView.setScaleType(ScaleType.FIT_CENTER);