如何在对话框式活动中将图像缩放到最大尺寸
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了一个解决方案,但我不完全确定它在任何给定情况下是否正确。
我创建了 ImageView (CustomImageView) 的子类并定义了 onMeasure() 方法,如下所示:
我的布局现在如下所示:
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:
My Layout looks as following now: