Glide 使 ImageView wrap_content 无用且没有使用目标的动画

Glide make ImageView wrap_content useless and no animatiion using target

我在我的项目中使用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不起作用,显示如下:

但是当我使用 :

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

ImageView 看起来:

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

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

三个问题:

  1. 为什么这两种方式会有区别?

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

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

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

  1. How can I make the ImageView's wrap_content useful when using the first method?

我设法通过将 adjustViewBounds 添加到 ImageView 的布局来使 wrap_content 工作:

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

我的 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);
    }