Glide使ImageView的wrap_content属性无效并且在使用target时没有动画效果。

13

我在我的项目中使用Glide加载图片,但我发现一个奇怪的问题。当我使用into(ImageView)时,ImageView显示的内容与使用into(new BitmapImageViewTarget(centerImageView))时不同:

这是我的布局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView
        android:id="@+id/center_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

当我使用时:

String imageUrl = "http://7xlz1k.com1.z0.glb.clouddn.com/feedback-201604229711.png";
Glide.with(this).load(imageUrl).into(centerImageView);

ImageView的wrap_content属性不起作用,显示效果如下:

enter image description here

但是当我使用以下代码:

    Glide.with(this).load(imageUrl).asBitmap().into(new BitmapImageViewTarget(centerImageView));

ImageView的样子如下:

enter image description here

imageUrl中包含的图片是一张120*120像素的图片。

当我使用BitmapImageViewTarget时,默认动画不起作用。

三个问题:

  1. 为什么这两种方法之间存在差异?

  2. 如何在使用第一种方法时使ImageView的wrap_content有效?

  3. 如何在使用BitmapImageViewTarget时启用默认动画?

2个回答

22

我将尝试为您的第二个问题提供解决方案:

  1. 当使用第一种方法时,如何使ImageView的wrap_content有效?

我成功让wrap_content起作用是通过在ImageView的布局中添加adjustViewBounds属性:

<ImageView
            android:id="@+id/center_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true" />

非常感谢。通过添加“adjustViewBounds”,它对我有用。祝您有美好的一天,先生 :) - Lee WonJoong

0

我的 Glide 版本:4.9.0

<!--  my ImageView:  -->
<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    public static void showRoundedCornerImage(ImageView imageView, 
                       String url, int maxHeight,int maxWidth,int corner) {
        Context context = imageView.getContext();
        corner = OsUtils.dp2px(context, corner);
        maxHeight = OsUtils.dp2px(context, maxHeight);
        maxWidth = OsUtils.dp2px(context, maxWidth);

        Glide.with(context)
                .load(url)
                .fitCenter()
                .transform(new RoundedCorners(corner))
                .override(maxWidth,maxHeight)// look here
                .into(imageView);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接