天天看點

glide使用bitmap之後占位圖不顯示

問題:因為要裁剪圖檔的是以使用了bitmap,但是使用之後占位圖不顯示了

原因:options的placeholder最終是不是要設定給imageview,但是你這裡又沒傳給他imageview,你傳的是SimpleTarget,那麼他上哪找imageview幫你自動設定placeholder

解決方法1:本人親測可行

在xml中直接設定src就行

<ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginRight="2dp"
                android:scaleType="centerCrop"
                android:src="@drawable/circle_icon_empty"
               />
           
options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
                .bitmapTransform(new CenterCrop())
                .placeholder(resHolderId)
                .error(resErrId)
                .dontAnimate();
        Glide.with(imageView.getContext()).load(url).apply(options).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                if (resource != null) {
                    BitmapDrawable bd = (BitmapDrawable) resource;
                    Bitmap bitmap = bd.getBitmap();
                    Bitmap endbitmap = BitmapCut.cutBitmap(bitmap); //調用裁剪圖檔工具類進行裁剪
                    if (bitmap != null)
                        imageView.setImageBitmap(endbitmap); //設定Bitmap到圖檔上
                }
            }
        });
           

解決方式 2:沒有測試,但是原理是可行

參考連結