Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android square imageview

im trying to make a table with square imageviews (and all with the same size). The problem is that the images have different height and width. The table is 3x3 and i'd like to: each cell have a with = 1/3 of the screen width (maybe with the layout weight?) and height =width Much better if there is a way of define that in the xml than in code. Please explain it because im a noob with layouts weights =( Thanks in advance,

like image 744
ChyBy Avatar asked Jul 11 '11 00:07

ChyBy


People also ask

How to make Square ImageView in android?

The best way for square image is use ConstraintLayout with constraintDimensionRatio Without give fix height/width. android:scaleType="centerCrop" is really a bad idea and screws up the pictures. This worked great; didn't do centerCrop, of course.

What is the ImageView in android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

What is adjustViewBounds in android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it. android:resizeMode—The resizeMode attribute is used to make a control resizable so we can resize it horizontally, vertically, or around both axes.


3 Answers

Try the scaleType attribute. You want centerCrop, I think.

like image 72
dmon Avatar answered Oct 14 '22 00:10

dmon


well you can also use scaleType=fitXY to squeeze each image into the imageview's dimensions

like image 44
Kevin Qiu Avatar answered Oct 13 '22 23:10

Kevin Qiu


Extending imageveiw might be the cleanest solution. This works for me:

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

    }

}

}

And in xml:

<com.your_package_name.Icon
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/screen" />
like image 31
Illegal Argument Avatar answered Oct 13 '22 23:10

Illegal Argument