Android ImageView: Fit Width

I upload an image and dynamically set it as the background of the screen using Imageview . I tried ScaleType to scale the image.

If the image height is greater than the width, then ScaleTypes fitStart , fitEnd and fitCenter do not work. Android will take the photo and place it depending on the height, but I see additional empty space as part of the width.

I want to zoom out the photo so that it matches the width, and I don't care if there is some extra blank space as part of the height or if the height is too long, is it okay if it comes out (if possible?).

ScaleType.XY scale the photo and fit everything into Imageview and does not care about the ratio of the height and weight of the image.

 <ImageView android:id="@+id/background" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:scaleType="fitStart" /> 
+48
android android-layout android-imageview
Aug 21 '12 at 16:35
source share
6 answers

I ended up using this code:

 <ImageView android:id="@+id/background" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/name" /> 

Make sure you set the image using src instead of background .

+139
Apr 3 '13 at
source share

android:adjustViewBounds="true" is doing the job!

+26
Sep 08 '14 at 15:01
source share

This elegant solution, found here , will work like a charm for you.

Basically, you just need to create a small class that extends ImageView and simply overrides the onMeasure method to adjust the width and height as you wish. Here it scales to fit the width using:

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); setMeasuredDimension(width, height); } 

You would use this special ImageView as follows:

 <your.activity.package.AspectRatioImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" android:src="@drawable/test" /> 
+12
Aug 22 '12 at 20:10
source share

There is a simple way, if you just want to fill the width of the screen and have a proportional height (and know the image ratio), you can do this in OnCreate ():

 setContentView(R.layout.truppview_activity); trupImage = (ImageView) findViewById(R.id.trupImage); Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); float scWidth = outMetrics.widthPixels; trupImage.getLayoutParams().width = (int) scWidth; trupImage.getLayoutParams().height = (int) (scWidth * 0.6f); 

Where 0.6 is the adjustment of the ratio (you can also automatically calculate, of course, if you know the w and h images).

PS:
Here is the XML side:

  <ImageView android:id="@+id/trupImage" android:layout_width="match_parent" android:background="@color/orange" android:layout_height="match_parent" android:adjustViewBounds="true" android:scaleType="fitCenter" /> 
+7
May 6 '13 at 11:32
source share

This works for me.

Create extends class ImageView

  package com.yourdomain.utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable d = getDrawable(); if (d != null) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } 

In xml instead of ImageView

use the following:
  <com.yourdomain.utils.ResizableImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/test" /> 
+3
Dec 29 '15 at 11:56
source share

I think that you cannot do this only with XML, you need to resize, bitmap

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); try { ((ImageView) findViewById(R.id.background)) .setImageBitmap(ShrinkBitmap(width)); } catch (FileNotFoundException e) { e.printStackTrace(); } 

then

 private Bitmap ShrinkBitmap(int width) throws FileNotFoundException { BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img, bmpFactoryOptions); int widthRatio = (int) android.util.FloatMath .ceil(bmpFactoryOptions.outWidth / (float) width); bmpFactoryOptions.inSampleSize = widthRatio; if (bmpFactoryOptions.inSampleSize <= 0) bmpFactoryOptions.inSampleSize = 0; bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; bmpFactoryOptions.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img, bmpFactoryOptions); return bitmap; } 

And the layout

  <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
+1
Aug 21 '12 at 16:56
source share



All Articles