【问题标题】:Stretch ImageView so that it fills the whole screen in a Dialog拉伸 ImageView 使其在 Dialog 中填满整个屏幕
【发布时间】:2014-07-24 09:58:09
【问题描述】:

我有一个应用程序,里面有很多小图像。为了提供“放大”功能,我想打开一个对话框(实现为带有对话框主题的Activity),其中只包含一个带有所选图片的 ImageView。

图像/对话框应尽可能大地显示(即与屏幕尺寸成比例),但也应尽可能大。这意味着对于横向图片,对话框也应该是横向格式,而不是填满整个屏幕(顶部/底部有黑条)。 基本上我希望对话框与 ImageView 和 ImageView 一样大(同时考虑包含图像的纵横比)。

有没有办法做到这一点?

请注意,显示的图像可能比可用屏幕空间小很多,但在这种情况下应该按比例放大。我无法提供不同分辨率/屏幕尺寸的图片,因为它是用户生成的内容。

我当前的对话框布局文件(适用于大于设备显示的图片,但不会放大较小的图片):

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@color/image_overlay" <!-- semi transparent background -->
             android:gravity="center"
             android:foregroundGravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:scaleType="centerInside"
        android:layout_margin="8dp"/>
</FrameLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    使用ScaleType.FIT_XY。请记住,这会破坏纵横比(因为图像会被拉伸)。 official definition

    在 X 和 Y 中独立缩放,以便 src 与 dst 完全匹配。这 可能会改变 src 的纵横比。

    例如在您的 Java 代码中,使用

    ImageView image = (ImageView) findViewById(R.id.img_background);
    imgview.setScaleType(ScaleType.FIT_XY);
    

    同样,您可以在您的 XML 中使用:

    <ImageView
        ...
        android:scaleType="fitXY" />
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    相关资源
    最近更新 更多