【问题标题】:how to keep image aspect ratio on API < 18如何在 API < 18 上保持图像纵横比
【发布时间】:2014-03-11 19:06:31
【问题描述】:

我有下一个 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

我使用 next 设置了正确的尺寸,但它不起作用:

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

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    为什么不使用

    android:scaleType="centerCrop"?
    

    编辑: 您无需在代码中执行任何其他操作。

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

    wrap_content 表示 View 将与图像一样小,并且不大于其父边界。

    fitXY 表示图像将被调整大小以适应整个 ImageView 而不会保留纵横比。 canterCrop 意味着

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

    所以也许你想试试

    android:scaleType="centerInside"
    

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

    ImageView scaleType

    【讨论】:

    • centerCrop 不起作用,图像的顶部和底部被“切割”,我不知道为什么,因为高度设置为 wrap_content。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2017-04-11
    • 2018-03-11
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多