【问题标题】:Ion sets the imageView height to a wrong valueIon 将 imageView 高度设置为错误值
【发布时间】:2014-10-03 23:10:08
【问题描述】:

我不知道为什么会这样:使用 Ion 库加载图像后,我的图像在图像下方和上方显示两个白条。我不想这样。

我的 ImageView 显示在列表视图项中。我的适配器代码如下所示:

if (node.getImageUrl() != null) { ivImage.setVisibility(View.VISIBLE); Ion.with(ivImage) .placeholder(R.drawable.anne_eli_icons_set_up_anne_eli_logo_530px) //.error(R.drawable.error_image) //.animateLoad(android.R.anim.cycle_interpolator) .animateIn(android.R.anim.fade_in) .load("http://app.anne-eli.at/" + node.getImageUrl().getUrlBig()); } 别的 { ivImage.setVisibility(View.GONE); }

我的 imageview 布局是这样的: <LinearLayout ... <ImageView android:id="@+id/fragment_appointment_list_item_article_iv_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/anne_eli_icons_set_up_anne_eli_logo_530px" android:background="@color/gray1" /> </LinearLayout>

有什么想法吗?

【问题讨论】:

    标签: android android-ion


    【解决方案1】:

    将您的 imageview 布局更改为这个:

    <LinearLayout ...
    <ImageView
            android:id="@+id/fragment_appointment_list_item_article_iv_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY" <!-- this line added , also your can use other value too like cropCenter ... -->
            android:src="@drawable/anne_eli_icons_set_up_anne_eli_logo_530px"
            android:background="@color/gray1" />
    </LinearLayout>
    

    【讨论】:

    • 我不想剪裁或合身。我只希望图像缩放到 100% 宽度并根据需要采用尽可能多的高度。问题是 imageViews 尺寸与图像不匹配。反之亦然。
    【解决方案2】:

    我认为它可能是“gray1”的任何值,但它看起来很白,因为它比背景中的图像更亮。去掉xml中的背景属性,就看不见了。

    同样,如果不对 ImageView 设置 scaleType,它将默认为 center。这将尝试调整图像大小,使其适合ImageView 的中心而不进行裁剪。 ImageView 绑定到其容器的宽度。提供的图像比这宽得多,因此它会缩小图像以适应。这意味着顶部和底部也将缩小,并且图像变得小于容器的高度。如果您希望图像靠近顶部,可以尝试将 scaleType 设置为 fitStart

    【讨论】:

    • 我删除了这个,因为我不确定 ion 是否是原因(高度应该调整大小以适应图像)。如果它有帮助,因为这是我唯一能想到的。
    • “图像变得小于容器的高度”:高度是 wrap_content,所以我看不出有任何原因为什么顶部和底部都可以看到灰色背景。我一开始没有背景,我只是把它放在那里让问题更明显。
    • wrap_content 仅表示视图被指定为它认为应该有的任何大小。换句话说,ImageView 被告知它的高度可以在 0 和它的容器的最大高度之间。仅仅因为支持位图较短并不意味着 ImageView 必须相应地调整它的高度(即使我们认为它应该)。因此,也许 ImageView 被编写为采用预先缩放的图像的高度。然后它会缩放图像,但不会调整 View 本身的高度。
    • 您可以尝试的一件事是将adjustViewBounds 尺寸设置为true。它旨在调整 ImageView 的边界以保持背景图像的纵横比。
    猜你喜欢
    • 2021-03-24
    • 2012-05-24
    • 2019-06-25
    • 2019-09-12
    • 2012-05-05
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多