Glide中的ColorDrawable占位符无法加载

3

我这里有问题吗?我想把那个灰色作为占位符加载,但是它没有加载。如果已经设置或加载了另一张图片,似乎只会出现这种情况。

Glide.with(getContext())
    .load(url)
    .placeholder(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray)))
    .error(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray)))
    .dontAnimate()
    .into(new SimpleTarget<GlideDrawable>() {
      @Override
      public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
        image.setImageDrawable(resource);
      }

      @Override
      public void onLoadFailed(Exception e, Drawable errorDrawable) {
        resetImageView();
      }
    });
1个回答

9
尝试以下代码:

Glide版本:

implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"></ImageView>
</RelativeLayout>

Java代码:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Glide.with(this)
                .load("http://matplotlib.org/_images/color_demo.png")
                .placeholder(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray)))
                .error(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray)))
                .dontAnimate()
                .into(imageView);

颜色(在colors.xml中)

<color name="placeholder_gray">#A9A9A9</color>

尝试使用有效的URL和无效的URL。对于有效的URL,在图像加载之前会出现灰色颜色,而对于无效的URL,则会出现灰色占位符。


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