ImageView in ListView that extends maximum width

i has a ListView , whose elements are ImageView s, for discussion. I want the image to scale to the width of the ListView strings and maintain its aspect ratio. here is a simplified version of the layout of the ListView element,

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:padding="4dp" > <FrameLayout android:id="@+id/page_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitCenter" android:src="@drawable/some_image" /> </FrameLayout> </FrameLayout> </LinearLayout> 

the width of the ImageView match_parent , and all its ancestors are the same. the layout designer shows that ImageView sizes span the entire width, but the image inside does not.

EDIT: The image is scaled correctly. the problem is that the height of the list line looks so that the width cannot be scaled to the desired size. if I use fitXY it scales the width, but then the aspect ratio is wrong.

any ideas? thanks.

+4
source share
2 answers

answering my own question ...

I could not find a way to do this without creating a custom view. in a nutshell, the custom view sets its own dimensions to the real (not scaled) dimensions of the extruded behind it.

 public class ResizableImageView extends ImageView { public static interface OnSizeChangedListener { void onSizeChanged(int width, int height); } private OnSizeChangedListener onSizeChangedListener = null; public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); setMeasuredDimension(width, height); if (onSizeChangedListener != null) { onSizeChangedListener.onSizeChanged(width, height); } } public void setOnSizeChangedListener(OnSizeChangedListener listener) { this.onSizeChangedListener = listener; } } 

in my case, I needed to get a modified value so that the size of the other representations, hence the listener interface.

+4
source

If you want to keep the proportions according to the Android API Guide , I think you need to change android: scaleType = "fitCenter" for android: scaleType = "centerInside" or maybe android: scaleType = "centerCrop", since fitCenter Doesn't support image aspect ratio.

0
source

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


All Articles