如何将参数传递给 Glide Callback 方法?

How to deliver parameters to Glide Callback method?

‍‍我用百度地图显示从服务器获取的商铺,包含图片url。我使用 Glide 为地图设置图标。

〉〉这是我在地图上添加标记的方法。

private void setMarks(List<ShopList> shops) {

    for(ShopList shopItem : shops){
        double latitude = shopItem.getLat();
        double longitude = shopItem.getLng();
        LatLng latLng = new LatLng(latitude,longitude);


        String shopName = shopItem.getName();
        OverlayOptions textOption = new TextOptions()
                .text(shopName)
                .fontSize(50)
                .position(latLng);
        mBaiduMap.addOverlay(textOption);


        Glide.with(mContext.getApplicationContext())
                .load(shopItem.getCategory_image())
                .asBitmap()
                .placeholder(R.drawable.ic_shop_image_loading) 
                .error(R.drawable.ic_shop_image_load_error)    
                .override(SizeUtils.dip2px(mContext,128),SizeUtils.dip2px(mContext,128)) 
                .centerCrop()                                                            
                .into(target);                                        
    }
}  

‍‍
这是 Glide 回调代码。

private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(resource);
        Marker marker = (Marker) mBaiduMap.addOverlay(new MarkerOptions().position(latLng).icon(descriptor));
        mMarkers.add(marker); 
    }

};  

‍‍我无法传递latLang的参数,所以我无法在onResourceReady中初始化Marker,也无法将Marker添加到mMarkers中。我该怎么做才能将 latLang 与特定位图相关联?

您必须创建自定义 Target

public class MyTarget extends SimpleTarget<Bitmap> {

    private final LatLng latLng;

    public MyTarget(LatLng latLng) {
        this.latLng = latLng;
    }

    @Override
    public void onResourceReady(final Bitmap resource, final GlideAnimation<? super Bitmap> glideAnimation) {
        // use your `latLng`
    }
}

并这样使用:

Glide.with(...)
    ...                                                    
    .into(new MyTarget(latLng));