Stretch Image to Fit Stretch Image to Fit android android

Stretch Image to Fit


I believe that is not possible, at least not with the options provided by scaleType attribute.
Your best option in this case is to use centerCrop, but only the center of the picture will be visible.

However, if you are not ok with this option, then you could scale the image programatically without loosing aspect ratio.In order to achieve this you'll need to calculate a scale factor based on the screen width, and then use this scale factor to know the new height of the image.

Like this:

ImageView imageView = (ImageView)findViewById(R.id.imageView);Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.foo);int imageWidth = bitmap.getWidth();int imageHeight = bitmap.getHeight();int newWidth = getScreenWidth(); //this method should return the width of device screen.float scaleFactor = (float)newWidth/(float)imageWidth;int newHeight = (int)(imageHeight * scaleFactor);bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);imageView.setImageBitmap(bitmap);

Also, you'll need to adjust the declaration of ImageView in layout file:

<ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />


android:adjustViewBounds="true" does the job!


use this MainImage.setScaleType(ImageView.ScaleType.FIT_XY);

OR you can simply add android:scaleType="fitXY" in xml.