我正在开发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
更多推荐
发布评论