缩放ImageView以适应屏幕宽度/高度,同时保持纵横比(Scale ImageView to fit screen width/height while maintaining aspect ra

编程入门 行业动态 更新时间:2024-10-28 14:29:20
缩放ImageView以适应屏幕宽度/高度,同时保持纵横比(Scale ImageView to fit screen width/height while maintaining aspect ratio)

我正在开发Xamarin上的移动应用程序。

在应用程序中,有一个ImageView显示的图像太小,无法适应屏幕的宽度。

我想缩放图像以适应屏幕的宽度,同时保持纵横比。

我的axml

<LinearLayout android:id="@+id/overlayImageContainer" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/overlayImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:scaleType="fitCenter"/> </LinearLayout>

结果

两个ImageViews显示a)比屏幕宽度宽的图片和b)比屏幕宽度窄的图片

我尝试过android:scaleType各种组合android:scaleType和android:adjustViewBounds="true"但没有成功。

I'm developing a mobile app on Xamarin.

In the app, there's an ImageView displaying an image that is too small to fit the width of the screen.

I want to scale the image to fit the width of the screen while maintaining the aspect ratio.

My axml

<LinearLayout android:id="@+id/overlayImageContainer" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/overlayImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:scaleType="fitCenter"/> </LinearLayout>

The result

Two ImageViews displaying a) a picture that is wider than the width of the screen and b) a picture that is narrower than the width of the screen

I've tried various combinations of android:scaleType and android:adjustViewBounds="true" without success.

最满意答案

事实证明它有点难,并且不可能开箱即用。 许多人 有 类似的 问题 。

我最终将@patrick-boos解决方案移植到.Net,如下所示:

扩展的ImageView类

using Android.Content; using Android.Util; using Android.Widget; namespace Nsa.BigBrotherUtility.Helpers { /// <summary> /// DynamicImageView is a helper extension that overrides OnMeasure in order to scale the said image /// to fit the entire width/or height of the parent container. /// </summary> public class DynamicImageView : ImageView { public DynamicImageView(Context context, IAttributeSet attributeSet) : base(context, attributeSet) { } protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.GetSize(widthMeasureSpec); int height = width * Drawable.IntrinsicHeight / Drawable.IntrinsicWidth; SetMeasuredDimension(width, height); } } }

我的axml

<Nsa.BigBrotherUtility.Helpers.DynamicImageView android:id="@+id/overlayImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" />

结果

600x600图像现在可以拉伸以填充屏幕宽度,同时保持纵横比

技术图纸被拉伸以适应屏幕宽度

It turned out to be a bit hard, and not possible out of the box. Many have similar problems.

I ended up porting @patrick-boos solution to .Net like so:

Extended ImageView Class

using Android.Content; using Android.Util; using Android.Widget; namespace Nsa.BigBrotherUtility.Helpers { /// <summary> /// DynamicImageView is a helper extension that overrides OnMeasure in order to scale the said image /// to fit the entire width/or height of the parent container. /// </summary> public class DynamicImageView : ImageView { public DynamicImageView(Context context, IAttributeSet attributeSet) : base(context, attributeSet) { } protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.GetSize(widthMeasureSpec); int height = width * Drawable.IntrinsicHeight / Drawable.IntrinsicWidth; SetMeasuredDimension(width, height); } } }

My axml

<Nsa.BigBrotherUtility.Helpers.DynamicImageView android:id="@+id/overlayImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" />

The result

The 600x600 image now stretches to fill width of screen while maintaining aspect ratio

technical drawing being stretched to fit width of screen

更多推荐

本文发布于:2023-07-31 14:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1344948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   宽度   屏幕   高度   以适应

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!