How to make a square of an android image?

I use ImageViewin an Android project.

Width: match_parent Height:wrap_content

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

enter image description here

+10
source share
8 answers

You can always try to customize your own sizes:

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

You will make it be the exact square in this way, but you will need to find the right sizes yourself. You can also try:

android:scaleType="fitXY"

Additional Information

scaleType fitXY . , ImageView ScaleType, . , , , .

+2

, 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" />
+25

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

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

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

    int width = getMeasuredWidth();
    setMeasuredDimension(width, width);
}
+6
source

Create your own imageView. Here is one example.

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);
    }
}
+2
source

Using

android:scaleType="fitCenter"

or

imageView.setScaleType(ScaleType.FIT_CENTER);
0
source

Determine your width / height (e.g. 40dp) and use centerCrop scaleType

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

you can use:

      app:layout_constraintDimensionRatio="1:1"

in this case:

<android.support.constraint.ConstraintLayout
      android:id="@+id/lyt_img_cover"
      android:layout_width="@dimen/image_top_episode"
      android:layout_height="match_parent"
      android:layout_centerInParent="false"
      android:elevation="0dp"
      android:foregroundGravity="center">

        <ImageView

          android:id="@+id/img_episode"
          android:layout_width="0dp"
          android:layout_height="0dp"
          android:elevation="7dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintDimensionRatio="1:1"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          />
    </ImageView>

its location should be ConstraintLayout

0
source

Source: https://habr.com/ru/post/1524324/


All Articles