imageview – Does android:scaleType="fitCenter" only work with fix Layout_width and Layout_height attributes?-ThrowExceptions

Exception or error:

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>
How to solve:

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>

###

<LinearLayout
    android:layout_height="wrap_content"

uses the original size of the image inside.

Try something bigger or fill_parent

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *