How to keep image aspect ratio by API <18

I have the following xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:gravity="center_vertical"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/my_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/image_areas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"
        android:background="#9a05e7"
        android:scaleType="fitXY"
        android:src="@drawable/mapa_mask"
        android:visibility="invisible"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"
        android:background="#fff"
        android:scaleType="fitXY"
        android:src="@drawable/mapa_default"
        android:visibility="visible"
        tools:ignore="ContentDescription" />
</RelativeLayout>

There are two overlay images.

If I test the application on devices that have installed Android 4.3 or more (> = API 18), the aspect ratio of the image remains flawless. But if I test the application on devices between API> = 14 API <= 17, the image is displayed a bit "narrow in height."

I used the following to set the correct sizes, but this did not work:

        // load the original BitMap (768 x 583 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
            R.drawable.mapa_default);
            // iv is an ImageView referenced in onCreate (mapa_default)
    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();
    int newWidth = iv.getWidth();
    int newHeight = heightRealImg;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
            height, matrix, true);

    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    // set the Drawable on the ImageView
    iv.setImageDrawable(bmd);

    // center the Image
    iv.setScaleType(ScaleType.FIT_XY);
    iv.setAdjustViewBounds(true);

This crash because iv.getWidth returns 0. I think this is because the layout has not finished loading yet.

How to keep image aspect ratio? Thanks

My decision:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center_vertical" >

<ImageView
    android:id="@+id/image_areas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:background="#9a05e7"
    android:scaleType="fitXY"
    android:src="@drawable/mapacyl_mask"
    android:visibility="invisible"
    tools:ignore="ContentDescription" />

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:scaleType="fitXY"
    android:src="@drawable/mapacyl"
    android:visibility="visible"
    tools:ignore="ContentDescription" />

</RelativeLayout>

"", . fitXY , ALTHOUGHT .

@Doctoror Drive

+3
1

android:scaleType="centerCrop"?

: .

wrap_content scale.

wrap_content , , , .

fitXY , , ImageView, . canterCrop

( ), ( ) ( ).

, ,

android:scaleType="centerInside"

( ), ( ) ( ).

ImageView scaleType

+3

All Articles