56

I have an Android Application, where I have an ImageView, I need to keep the size constant, there can be various images that I need to put into this ImageView, of different sizes.

I just need to ensure that all the Images must fit into the ImageView, the Image should increase in size if it is smaller and should decrease, in case it is bigger.

Thanks a lot for your time.

Swati Garg
  • 995
  • 1
  • 10
  • 21
Shalabh
  • 687
  • 1
  • 6
  • 11

7 Answers7

88

Fix ImageView's size with dp or fill_parent and set android:scaleType to fitXY.

Cesare
  • 9,139
  • 16
  • 78
  • 130
Youngsik Oh
  • 911
  • 7
  • 2
56

In your case you need to

  1. Fix the ImageView's size. You need to use dp unit so that it will look the same in all devices.
  2. Set android:scaleType to fitXY

Below is an example:

<ImageView
    android:id="@+id/photo"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:src="@drawable/iclauncher" 
    android:scaleType="fitXY"/>

For more information regarding ImageView scaleType please refer to the developer website.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
17

Try this

ImageView img
    Bitmap bmp;
    int width=100;
    int height=100;
    img=(ImageView)findViewById(R.id.imgView);
    bmp=BitmapFactory.decodeResource(getResources(),R.drawable.image);//image is your image                                                            
    bmp=Bitmap.createScaledBitmap(bmp, width,height, true);
    img.setImageBitmap(bmp);

Or If you want to load complete image size in memory then you can use

<ImageView
    android:id="@+id/img"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/image" 
    android:scaleType="fitXY"/>
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
7

You can also try this, suppose if you want to make a back image button and you have "500x500 png" and want it to fit in small button size.

Use dp to fix ImageView's size.

add this line of code to your Imageview.

android:scaleType="fitXY"

EXAMPLE:

<ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imageView2"
        android:src="@drawable/Backicon"
        android:scaleType="fitXY"
        />
Community
  • 1
  • 1
Nikhil jassal
  • 149
  • 2
  • 4
1

I had the same issue and this helped me.

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="fitXY"
    />
0

You need to add android:scaleType and set fitXY in XML unless you add layout_width & layout_height attributes.
Example:
android:scaleType="fitXY"

If you want to add programmatically,
Example:
youriv.setScaleType(ImageView.ScaleType.FIT_XY);

0

There is not a clear answer yet. ImageView is not resizing to image size every image loads.

fkybrd
  • 11
  • 3