Glide 4.7.1占位符未加载

时间:2018-08-17 08:01:00

标签: android placeholder android-glide

我正在尝试使用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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用Glide(4.7.1)

Glide.with(getActivity()).load(avatar)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.defaultusericon))
                .into(mUserIcon);