无法使用 Glide v4 加载位图

Unable to load bitmap using Glide v4

我正在使用 Glide v4 加载位图,然后可以将其用作地图上的标记。当我像这样使用已弃用的 SimpleTarget 时,一切正常。

GlideApp.with(getContext()).asBitmap().load(url)
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

            // load bitmap as marker
        }
    });

当我尝试删除已弃用的代码并使用如下所示的 Target<Bitmap> 时,我可以看到 onLoadStarted 被调用,但 onResourceReady 从未被调用,[=16= 也没有被调用].

GlideApp.with(getContext()).asBitmap()
    .load(UrlHelper.createUrl(poi.getMapMarker()))
    .into(marketBitmap);

private Target<Bitmap> marketBitmap = new Target<Bitmap>() {
    @Override
    public void onLoadStarted(@Nullable Drawable placeholder) {
        Log.d("GlideMar", "marker load started");
    }

    @Override
    public void onLoadFailed(@Nullable Drawable errorDrawable) {
        Log.e("GlideMar", "marker load failed");
    }

    @Override
    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
        Log.d("GlideMar", "onResourceReady");
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {
        Log.d("GlideMar", "marker onLoadCleared");
    }

    @Override
    public void getSize(@NonNull SizeReadyCallback cb) {

    }

    @Override
    public void removeCallback(@NonNull SizeReadyCallback cb) {

    }

    @Override
    public void setRequest(@Nullable Request request) {

    }

    @Nullable
    @Override
    public Request getRequest() {
        return null;
    }

    @Override
    public void onStart() {
        Log.d("GlideMar", "marker onStart");
    }

    @Override
    public void onStop() {
        Log.d("GlideMar", "marker onStop");
    }

    @Override
    public void onDestroy() {
        Log.d("GlideMar", "marker onDestroy");
    }
};

来自 Glide Custom Targets 文档。

If you’re using a custom Target and you’re not loading into a View that would allow you to subclass ViewTarget, you’ll need to implement the getSize() method.

所以在你的情况下,只需将下面的代码放在 getSize 方法中

@Override
public void getSize(SizeReadyCallback cb) {
    cb.onSizeReady(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}

现在 onResourceReady 方法将在您 运行 应用程序时调用。