Android ImageView Bitmap FitXY settings not working

I tried for a very long time to set the bitmap to fit the borders of the ImageView .

It just doesn't work through scaleType: fitXY . The size of the bitmaps is lower than the ImageView (saved at 100X100 pixels), I want my bitmaps to fit and stretch in the ImageView .

Here is the code:

 <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageView" android:layout_height="match_parent" android:layout_width="match_parent" android:src="@drawable/locked_image" android:scaleType="fitXY" /> 

Here is the ImageView installation code (I install scaleType in different places for testing):

 @Override public View getView(int position, View convertView, ViewGroup parent) { final ImageView imageView; if (convertView == null) { imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false); imageView.setScaleType(ScaleType.FIT_XY); } else { imageView = (ImageView) convertView; imageView.setScaleType(ScaleType.FIT_XY); } imageLoader.displayImage(imagesIds[position], imageView, options); imageView.setScaleType(ScaleType.FIT_XY); return imageView; } 

Here's what it looks like:

enter image description here

As you can see, the height of the ImageView beautiful, but I want its width to be the same. I do not have access to my bitmap through my activity, so please do not ask me to resize the bitmap. I really don’t understand why this just doesn’t work as it should.


EDIT:
Just to make the problem more clear. ImageView height is calculated for each device. For each device, this is different. I want the width of my ImageView to be the same as the height. I use the Two-WayGridView to display images, if that matters.

+4
source share
4 answers

Using scaleType will not resize your ImageView, as you seem to think it should. All he does is scale the bitmap to the size of the ImageView (which is not bad, judging by your screenshot).

If you want to make square ImageViews, you should try this question .

Also, I'm not sure I understand why you are using the TwoWayGridView library. It is specially designed so that you can specify the number of rows / columns and it will stretch the elements to fit. This seems to be the exact opposite of what you want, since you want to make them square. A regular GridView will work if you override onMeasure() each element, as shown in the related question / answer.

+9
source

I wonder which container widget you use to store ImageView.
e.g. ... GridView, ListView, etc.

If you are using a GridView, you are trying to set the number of columns using GridView.setNumColumns ().

If still not working, you are trying to use the AspectRatioFrameLayout class in one of many ways.
this class is simple. follow these steps!


1. Add attributes to res / attrs.xml

 <declare-styleable name="AspectRatioFrameLayout"> <attr name="aspectRatio" format="float" /> </declare-styleable> 


2. create an AspectRatioFrameLayout class

 public class AspectRatioFrameLayout extends FrameLayout { private static final String TAG = "AspectRatioFrameLayout"; private static final boolean DEBUG = false; private float mAspectRatio; // width/height ratio public AspectRatioFrameLayout(Context context) { super(context); } public AspectRatioFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); initAttributes(context, attrs); } public AspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initAttributes(context, attrs); } private void initAttributes(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AspectRatioFrameLayout); mAspectRatio = typedArray.getFloat(R.styleable.AspectRatioFrameLayout_aspectRatio, 1.0f); typedArray.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (DEBUG) Log.d(TAG, "widthMode:"+widthMode+", heightMode:"+heightMode); if (DEBUG) Log.d(TAG, "widthSize:"+widthSize+", heightSize:"+heightSize); if ( widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY ) { // do nothing } else if ( widthMode == MeasureSpec.EXACTLY ) { heightMeasureSpec = MeasureSpec.makeMeasureSpec((int)(widthSize / mAspectRatio), MeasureSpec.EXACTLY); } else if ( heightMode == MeasureSpec.EXACTLY ) { widthMeasureSpec = MeasureSpec.makeMeasureSpec((int)(heightSize * mAspectRatio), MeasureSpec.EXACTLY); } else { // do nothing } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 


3. Modify list_item_image.xml

 <your.package.AspectRatioFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" app:aspectRatio="1"> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageView" android:layout_height="match_parent" android:layout_width="match_parent" android:src="@drawable/locked_image" android:scaleType="fitXY" /> </your.package.AspectRatioFrameLayout> 
+3
source

Instead of using the src property, use the background property for ImageView. The bitmap will be stretched.

 <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageView" android:layout_height="match_parent" android:layout_width="match_parent" android:background="@drawable/locked_image" android:scaleType="fitXY" /> 
+1
source

Use background with fitxy instead of src. It will stretch your image:

 android:background="@drawable/locked_image" android:scaleType="fitXY" 
-one
source

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


All Articles