2015-10-19 151 views
3

我正在Xamarin上开发移动应用程序。Scale ImageView适合屏幕宽度/高度,同时保持宽高比

在应用程序中,有一个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显示)一个图像比所述屏幕的宽度宽,和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

我试过android:scaleTypeandroid:adjustViewBounds="true"没有成功的各种组合。

回答

3

事实证明有点困难,而且不可能开箱即用。 Manyhavesimilarproblems

我结束了移植@patrick-boos solution到.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" 
/> 

结果

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

technical drawing being stretched to fit width of screen

相关问题