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,
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.
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
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.
Try the scaleType attribute. You want centerCrop
, I think.
well you can also use scaleType=fitXY to squeeze each image into the imageview's dimensions
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With