如何在API<18上保持图像宽高比

3

我有以下的XML布局:

<?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>

有两张图片“叠加”。

如果我在已安装了Android 4.3或更高版本(>= API 18)的设备上测试应用程序,则图像纵横比保持完美。但是,如果我在API> = 14 API <= 17之间的设备上测试应用程序,则图像显示为“高度狭窄”。

我使用以下代码来设置正确的大小,但它没有起作用:

        // 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);

这个程序崩溃是因为iv.getWidth返回了0。我认为这是由于布局还没有加载完成。

我该如何保持图像的长宽比呢?谢谢。

我的解决方案:

<?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,图像会扩展并适应所有屏幕尺寸,尽管比例不会保持一致。
非常感谢@Doctoror Drive。
1个回答

3
为什么不使用
android:scaleType="centerCrop"?

编辑:

在代码中不需要做任何其他操作。

您误解了wrap_content和scale类型的含义。

wrap_content表示视图将与图像一样小,但不会大于其父级边界。

fitXY表示图像将被调整大小以适合整个ImageView,而不保留纵横比。

centerCrop的意思是

均匀缩放图像(保持图像的纵横比),使图像的宽度和高度都等于或大于视图相应的尺寸(减去填充)。

因此,也许您想尝试

android:scaleType="centerInside"

等比例缩放图像(保持图像的纵横比),使得图像的宽度和高度都等于或小于视图相应的维度(减去填充)。

ImageView的scaleType属性


centerCrop不起作用,图像的顶部和底部被“裁剪”了,我不知道为什么,因为高度设置为wrap_content。谢谢您的时间。 - Adrian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接