如何在对话框式活动中将图像缩放到最大尺寸

发布于 2024-10-04 18:13:26 字数 1036 浏览 2 评论 0原文

我有一个使用 android:theme="@android:style/Theme.Dialog" 的活动。该活动包含一个 ImageView,它可以显示不同尺寸的图像。 ImageView 应缩放图像,使其完全适合窗口,至少在轴上完全适合并保持原始纵横比。对话框的大小只能与 ImageView 一样大(加上其边框宽度)。

我尝试了以下方法,但对话框窗口始终使用整个可用空间:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageParent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip">
    <ProgressBar
        android:id="@android:id/progress"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_centerInParent="true"
        android:visibility="gone"
        style="@android:attr/progressBarStyleSmallTitle"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/image"/>
</RelativeLayout>

I have an activity which uses android:theme="@android:style/Theme.Dialog". The activity contains an ImageView which can show images of different dimensions. The ImageView should scale the Image so that it entirely fits into the window, at least on axes fits entirely and maintains the original aspect ratio. The size of the dialog should only be as large as the ImageView (plus its border width).

I tried the following but the dialog window always uses the whole available space:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageParent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip">
    <ProgressBar
        android:id="@android:id/progress"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_centerInParent="true"
        android:visibility="gone"
        style="@android:attr/progressBarStyleSmallTitle"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/image"/>
</RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-10-11 18:13:26

我自己找到了一个解决方案,但我不完全确定它在任何给定情况下是否正确。
我创建了 ImageView (CustomImageView) 的子类并定义了 onMeasure() 方法,如下所示:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable drawable = getDrawable();
    if (drawable != null) {
        int measureHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        int measureWidth = View.MeasureSpec.getSize(widthMeasureSpec);

        int resultWidth = measureWidth;
        float scaleFactor = (float)resultWidth / drawable.getMinimumWidth();
        int resultHeight = (int)(scaleFactor * drawable.getMinimumHeight());

        if (resultHeight > measureHeight) {
            resultHeight = measureHeight;
            scaleFactor = (float)resultHeight / drawable.getMinimumHeight();
            resultWidth = (int)(scaleFactor * drawable.getMinimumWidth());
        }

        setMeasuredDimension(resultWidth, resultHeight);
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

我的布局现在如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageParent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip">
    <ProgressBar
        android:id="@android:id/progress"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_centerInParent="true"
        style="@android:attr/progressBarStyleSmallTitle"
        android:visibility="gone"/>
    <com.example.CustomImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/image"/>
</RelativeLayout>

I found a Solution by myself but I'm not totally sure if it does the correct thing at any given situation.
I created a subclass of ImageView (CustomImageView) and defined the onMeasure() method as following:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable drawable = getDrawable();
    if (drawable != null) {
        int measureHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        int measureWidth = View.MeasureSpec.getSize(widthMeasureSpec);

        int resultWidth = measureWidth;
        float scaleFactor = (float)resultWidth / drawable.getMinimumWidth();
        int resultHeight = (int)(scaleFactor * drawable.getMinimumHeight());

        if (resultHeight > measureHeight) {
            resultHeight = measureHeight;
            scaleFactor = (float)resultHeight / drawable.getMinimumHeight();
            resultWidth = (int)(scaleFactor * drawable.getMinimumWidth());
        }

        setMeasuredDimension(resultWidth, resultHeight);
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

My Layout looks as following now:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageParent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip">
    <ProgressBar
        android:id="@android:id/progress"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_centerInParent="true"
        style="@android:attr/progressBarStyleSmallTitle"
        android:visibility="gone"/>
    <com.example.CustomImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:id="@+id/image"/>
</RelativeLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文