Glide 4.7.1 占位符不加载

5
我正在尝试使用GlideLibrary加载占位图像,并使用SimpleTarget。然而,占位图像没有被加载,但来自URL的图像已经被加载了。我已经进行了大量搜索并使用了RequestOptions,但仍然无法正常工作。
以下是代码:
Glide.with(mContext).load("https://i.imgur.com/0k7ZFWr.png").apply(RequestOptions.placeholderOf(R.drawable.pubgguide_icon)).into(new SimpleTarget<Drawable>() {
        @Override
        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                holder.cardImageLayout.setBackground(resource);
                Drawable cardViewBackground = holder.cardImageLayout.getBackground();
                cardViewBackground.setColorFilter(0x5F000000, PorterDuff.Mode.SRC_ATOP);
            }
        }
    });

我是使用Glide库4.7.1。我已经尝试使用setDefaultRequestOptions(),但并没有起作用。
我的cardview XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/category_cardview"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="@dimen/leftrightmargin"
        android:layout_marginRight="@dimen/leftrightmargin"
        android:layout_marginTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="4dp"
                android:layout_height="match_parent"
                android:background="#FFBF41"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/cardImageLayout"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="2">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="3dp"
                        android:layout_marginLeft="@dimen/leftrightmargin"
                        android:layout_marginRight="@dimen/leftrightmargin"
                        android:layout_marginTop="60dp"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/category_name"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_gravity="bottom"
                            android:layout_weight="1"
                            android:gravity="bottom"
                            android:paddingLeft="5dp"
                            android:shadowColor="#f3848383"
                            android:shadowDx="4"
                            android:shadowDy="4"
                            android:shadowRadius="2"
                            android:text="WEAPONS"
                            android:textColor="@color/colorWhite"
                            android:textSize="30sp" />

                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.3"
                    android:background="#000"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1">

                        <TextView
                            android:id="@+id/category_desc"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:paddingLeft="11dp"
                            android:text="Know your weapons before you shoot"
                            android:textColor="@color/colorLightWhite"
                            android:textScaleX="1" />
                    </LinearLayout>

                    <com.romainpiel.shimmer.ShimmerTextView
                        android:id="@+id/arrow_text"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_weight="0.2"
                        android:gravity="right"
                        android:text=">>>>>"
                        android:textColor="#000"
                        android:textScaleX="1.5"
                        android:textStyle="bold"
                        app:reflectionColor="#FFBF41" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>
</FrameLayout>

你实际想要做什么?在图片未正确加载之前加载占位符吗? - Devil10
2个回答

2
你可以使用Glide(4.7.1)实现此功能。
Glide.with(getActivity()).load(avatar)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.defaultusericon))
                .into(mUserIcon);

1

placeHolderOf 只有在您将其加载到 ImageView 中时才设置占位符图像。如果您想将占位符设置为 ViewGroup,则 SimpleTarget 类提供了一个方法,您可以重写该方法,在图像开始加载时设置背景。
在您的情况下,请尝试像下面这样加载:

Glide.with(mContext)
            .load("https://i.imgur.com/0k7ZFWr.png")
            .apply(RequestOptions.placeholderOf(R.drawable.pubgguide_icon))
            .into(new SimpleTarget<Drawable>() {

                @Override
                public void onLoadStarted(@Nullable Drawable resource) {
                    holder.cardImageLayout.setBackground(resource);
                }

                @Override
                public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        holder.cardImageLayout.setBackground(resource);
                        Drawable cardViewBackground = holder.cardImageLayout.getBackground();
                        cardViewBackground.setColorFilter(0x5F000000, PorterDuff.Mode.SRC_ATOP);
                    }
                }
            });

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