【问题标题】:How to deliver parameters to Glide Callback method?如何将参数传递给 Glide 回调方法?
【发布时间】:2017-04-04 17:59:37
【问题描述】:

  我用百度地图显示从服务器获取的店铺,包含图片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 与特定位图关联起来?

【问题讨论】:

    标签: android android-glide baidu-map


    【解决方案1】:

    您必须创建您的自定义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));
    

    【讨论】:

    • 感谢回答。我对封装有深入的了解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多