Android image

im trying to create a table with square images (and all with the same size). The problem is that the images have different heights and widths. The table is 3x3, and I would like: each cell has c = 1/3 of the width of the screen (perhaps with a layout weight?) And height = width It is much better if there is a way to determine what is in xml than in code. Please explain this because im noob with scale models = (Thanks in advance,

+7
source share
4 answers

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

+4
source

you can also use scaleType = fitXY to compress each image into image sizes

+1
source

The imageveiw extension may 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" /> 
+1
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

All Articles