缩放ImageView以适应屏幕宽度/高度,同时保持纵横比

时间:2015-10-19 08:29:04

标签: android xamarin

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

在应用程序中,有min.Dec <= star[i].Dec <= max.Dec显示的图像太小,无法适应屏幕宽度。

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

我的ImageView

axml

结果

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

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

我尝试了<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> android:scaleType的各种组合但没有成功。

1 个答案:

答案 0 :(得分:3)

事实证明它有点难,并且不可能开箱即用。 Many have similar problems

我最终将@patrick-boos solution移植到.Net,如此:

扩展ImageView类

android:adjustViewBounds="true"

我的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

结果

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

technical drawing being stretched to fit width of screen