【问题标题】:Scale ImageView to fit screen width/height while maintaining aspect ratio缩放 ImageView 以适应屏幕宽度/高度,同时保持纵横比
【发布时间】:2016-01-17 12:46:00
【问题描述】:

我正在 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>

结果

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

我尝试了android:scaleTypeandroid:adjustViewBounds="true" 的各种组合,但均未成功。

【问题讨论】:

    标签: android xamarin


    【解决方案1】:

    结果有点难,而且不可能开箱即用。 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"
    />
    

    结果

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

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2016-05-18
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多