在我的ClusterRenderer中,我试图在应用程序中更改Google地图上的图钉图标,但是我没有将图标保存在drawable文件夹中,而是必须从URL加载它们。为此,我使用的是Picasso库,但问题是图标在首次加载时不会更改。第二次工作正常。
这是我尝试更改图标的代码:
public class MyRenderer extends DefaultClusterRenderer<MyClusterItem> {
private Set<Target> targetsProtectedFromGarbageCollection = new HashSet<>();
@Override
protected void onBeforeClusterItemRendered(final MyClusterItem item, final MarkerOptions markerOptions) {
try {
if(item.getPinUrl()!=null){
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(iconBitmap));
targetsProtectedFromGarbageCollection.remove(this);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
targetsProtectedFromGarbageCollection.remove(this);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
targetsProtectedFromGarbageCollection.add(target);
Picasso.with(mContext).load(item.getPinUrl()).into(target);
}else{
// case when not loading icon from url but get it from drawable folder
}
} catch (RuntimeException e) {
Log.d("MyRenderer", "Could not set: " + item.toString());
}
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
我使用集合targetsProtectedFromGarbageCollection
来强烈引用Target
,因为在遇到问题之前根本没有调用onBeforeClusterItemRendered
。找到解决方案here。现在onBeforeClusterItemRendered
已被正确调用,但图标未更改。