22

I use Layout_width="fill_parent" and Layout_height="wrap content". If an image is bigger than the ImageView, it will be downscaled perfectly.

However, I never got it working to upscale smaller images. I tried any combination of ScaleType and "AdjustViewBounds": it always stays in its own size in the middle of the image view. Here is the code...

 <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content"
        android:background="@drawable/content_bg"
        android:orientation="vertical"
        android:padding="5dp"
        android:layout_width="fill_parent"
        android:layout_marginLeft="1px">
        <ImageView
          android:layout_height="wrap_content"
          android:src="@drawable/test"
          android:id="@+id/ImageViewTest"
          android:layout_width="fill_parent"
          android:adjustViewBounds="true"
          android:scaleType="center"></ImageView>
</LinearLayout>
OneWorld
  • 17,512
  • 21
  • 86
  • 136
  • Try using CENTER_INSIDE or FIT_XY. – bhups Sep 24 '10 at 09:02
  • I tried all of them. None of them worked for me. – OneWorld Sep 24 '10 at 09:12
  • 4
    I found the only way to get it working, was setting layout_hight and layout_width to fixed values like 150dp x 200dp – OneWorld Nov 05 '10 at 21:39
  • I am not able to test the given answer(s) anymore, since I stopped developing Android Apps. Please suggest if I should mark an answer done, if you have tested it and matches the requirements of my question. – OneWorld Jan 22 '13 at 21:30

4 Answers4

9

I just found a way to make it work.

This works in 2.1(AVD) & 4.1.2(device)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter" />

</LinearLayout>
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Jack L
  • 91
  • 1
  • 1
  • 1
    with this method, if you have a border on the imageView, there'll be whitespace on sides where the image didin't scale to – Olumide Jun 25 '14 at 23:12
5
<LinearLayout
    android:layout_height="wrap_content"

uses the original size of the image inside.

Try something bigger or fill_parent

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • This is just one of my golden answers, you have no idea of how much trouble I've had with imageview and its faulty (or misunderstood) scaletype. Thamls a ton. – Warpzit Dec 04 '11 at 21:06
2

Use android:scaleType="FIT_START" or android:scaleType="FIT_END", to Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst (from GoogleDev).

More info:

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Nik NexAndrX
  • 302
  • 1
  • 6
  • 2
    It's android:scaleType="fitStart" or android:scaleType="fitEnd", and these are almost exactly the same as android:scaleType="fitCenter" anyway. The difference is how they handle the empty space, aliging to start, end, or centering. – OldSchool4664 Dec 12 '12 at 01:01
1

Adding android:scaleType="fitXY" to ur ImageView should stretch the image if the size is small.

DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • Yeah it works fine if image is larger but if its smaller then it only fits either height or width. Stupid the way Android handles images – JPM Mar 25 '15 at 20:55