Stretch image in ImageView

I have an ImageView that has a height and width set to "fill_parent" using a Relative Layout that has the same values. Set scale scaleType = "fitXY"

Here's the XML layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="@drawable/background_image" /> </RelativeLayout> 

Sets the width and height, but the image is stretched.

+7
android imageview
source share
3 answers

What fitXY should do. You can try centerInside if you do not want to crop the image or centerCrop if you want to fill the entire space (and crop the image).

Here is a good list with examples to better understand scale types:

http://etcodehome.blogspot.it/2011/05/android-imageview-scaletype-samples.html

+10
source share

These two properties solved the problem for me:

 android:scaleType="fitXY" android:adjustViewBounds="true" 
+3
source share
 package utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; /** * Created by Navneet Boghani on 7/7/16. */ public class ProportionalImageView extends ImageView { public ProportionalImageView(Context context) { super(context); } public ProportionalImageView(Context context, AttributeSet attrs) { super(context, attrs); } public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable d = getDrawable(); if (d != null) { // int w = MeasureSpec.getSize(widthMeasureSpec); // int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth(); int h = MeasureSpec.getSize(heightMeasureSpec); int w = h * d.getIntrinsicWidth() / d.getIntrinsicHeight(); setMeasuredDimension(w, h); } else super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 
0
source share

All Articles