【问题标题】:Glide not loading image without placeholder滑翔不加载没有占位符的图像
【发布时间】:2017-07-12 22:39:56
【问题描述】:

我正在尝试使用 Glide 加载图像:

case "image":
    Log.d("TRecyViewAdapter", "Got Image");
    ImageView imageView = new ImageView(context);
    imageView.setLayoutParams(new    GridLayoutManager.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT
    ));
    contents.addView(imageView);
    Glide.with(context).load(part.getContent()).into(imageView);
    break;

什么都没显示。

但是,如果我将最后一行更改为毕加索:

    Picasso.with(context).load(part.getContent()).into(imageView);

图片按照我的预期以全尺寸加载。

还有。如果我将 Glide 与占位符一起使用:

    Glide.with(context).load(part.getContent()).placeholder(R.mipmap.def_avatar).into(imageView);

它加载图像但尺寸非常小(R.mipmap.def_avatar 的大小)

我希望能够加载实际大小的图像而无需指定占位符(它是一个动态视图,它应该显示许多不同大小的不同图像等等)

我想使用 Glide 而不是 Picasso,因为我想要支持动画 gif。

【问题讨论】:

  • 我忘了说图片是从互联网加载的。这不是本地图像。
  • 可能是因为你动态添加了视图。尝试设置ImageView的固定高度和宽度
  • 是的,看起来像添加固定大小显示图像。问题是我无法事先知道图像会有多大。
  • 你应该使用xml中的ImageView,第二件事的内容是RelaiveLayout还是什么?
  • 如果从 xml 中,父(内容)是一个 LinearLayout 并且我不知道它将包含多少个 ImageViews 或 TextViews 或这些图像的大小,我无法做到

标签: android picasso android-glide


【解决方案1】:

看起来添加覆盖就可以了。图像现在按预期加载。

Glide.with(context).load(part.getContent()).override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView);

【讨论】:

  • 我也有这种行为。这是错误还是功能? :D
【解决方案2】:

我刚刚为自己解决了这个问题,在 XML 中的 ImageView 上设置了以下属性对我有用:

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

</FrameLayout>

这里也是我的Android代码供参考:

public class MyCardViewHolder extends CardViewHolder {

    public MyCardViewHolder(View inflated) {
        super(inflated);
    }

    public void bind(MyCard card) {
        ImageView imageView = ((ImageView) itemView.findViewById(R.id.image));

        Glide.with(itemView.getContext())
                .load(card.backgroundImageUrl())
                .into(imageView);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2020-07-19
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多