任何人请告诉我如何延迟加载标记,因为我的所有标记图像都来自服务器。我可以从服务器下载它,但只有在有地图移动的情况下才会显示它。即标记图像只有在已经缓存的情况下才会更新,对象引用将不再起作用,谷歌地图将标记视图转换为位图并进行设置。如果用户使地图保持空闲,则它不会加载图像。 :(
我花了差不多一个星期的时间尽可能地做到了,但是它已经完成了。如果有人能给出一个火花,那将真的很有帮助。
public class ROverViewRender extends DefaultClusterRenderer<RItem> {
private IconGenerator mIconGenerator;
private IconGenerator mClusterIconGenerator;
private RImageView mImageView;
private RImageView mImageVieStar;
private ImageView mClusterImageView;
private Context context;
private ClusterManager<RItem> mClusterManager;
private ImageLoader imageLoader;
public ROverViewRender(Context context, GoogleMap map, ClusterManager<RItem> clusterManager) {
super(context, map, clusterManager);
this.mClusterManager=clusterManager;
Activity activity = (Activity) context;
this.context=context;
mClusterIconGenerator=new IconGenerator(context);
View clustorIcon = activity.getLayoutInflater().inflate(R.layout.clustor_icon, null);
mClusterIconGenerator.setContentView(clustorIcon);
mIconGenerator=new IconGenerator(context);
View markerIcon = activity.getLayoutInflater().inflate(R.layout.marker_icon, null);
mIconGenerator.setContentView(markerIcon);
mImageView=(RImageView)markerIcon.findViewById(R.id.marker_image);
mImageVieStar=(RImageView)markerIcon.findViewById(R.id.marker_route_rating);
mClusterImageView=(NetworkImageView)clustorIcon.findViewById(R.id.cluster_image);
imageLoader=VolleySingleton.getInstance(context).getImageLoader();;
}
@Override
protected void onBeforeClusterItemRendered(final RItem item, MarkerOptions markerOptions) {
setMarkerImage(item,markerOptions);
Bitmap icon = mIconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected void onBeforeClusterRendered(Cluster<RItem> cluster, MarkerOptions markerOptions) {
Drawable drawable =context.getResources().getDrawable(R.drawable.ic_launcher);
mClusterImageView.setImageDrawable(drawable);
Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
@Override
protected void onClusterItemRendered(RItem clusterItem, Marker marker) {
RLog.e("Invoked the ....>>>>>");
// setMarkerImage(clusterItem);
}
private void setMarkerImage(RItem item,final MarkerOptions markerOptions){
RLog.e("Image setter invoked..."+item.getMarkerUrl());
mImageView.setImageUrl(item.getMarkerUrl(), imageLoader);
imageLoader.get(item.getMarkerUrl(), new ImageListener() {
public void onErrorResponse(VolleyError error) {
// imageView.setImageResource(R.drawable.icon_error); // set an error image if the download fails
}
public void onResponse(ImageContainer response, boolean arg1) {
RLog.e("Image download completed...");
if (response.getBitmap() != null) {
mImageView.setImageBitmap(response.getBitmap());
}
}
});
}
}
答案 0 :(得分:0)
我编写了示例代码,可能会有用。 我使用Picasso作为图像加载库。
class MyClusterRenderer extends DefaultClusterRenderer<MyClusterItem> {
private final IconGenerator iconGenerator;
private final IconGenerator clusterIconGenerator;
private final ImageView imageView;
private final ImageView clusterImageView;
private final int markerWidth;
private final int markerHeight;
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MyClusterItem> clusterManager) {
super(context, map, clusterManager);
// initialize cluster icon generator
clusterIconGenerator = new IconGenerator(context.getApplicationContext());
View clusterView = LayoutInflater.from(context).inflate(R.layout.marker_cluster, null);
clusterIconGenerator.setContentView(clusterView);
clusterImageView = (ImageView) clusterView.findViewById(R.id.image);
// initialize cluster item icon generator
iconGenerator = new IconGenerator(context.getApplicationContext());
imageView = new ImageView(context.getApplicationContext());
markerWidth = (int) getResources().getDimension(R.dimen.marker_width);
markerHeight = (int) getResources().getDimension(R.dimen.marker_height);
imageView.setLayoutParams(new ViewGroup.LayoutParams(markerWidth, markerHeight));
int padding = (int) getResources().getDimension(R.dimen.marker_space);
imageView.setPadding(padding, padding, padding, padding);
iconGenerator.setContentView(imageView);
}
@Override protected void onBeforeClusterItemRendered(MyClusterItem item,
MarkerOptions markerOptions) {
loadAsync(imageView, item.getImageUrl());
try {
Bitmap icon = iconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
BitmapUtils.recycle(icon);
} catch (Exception e) {
// FIXME workaround : called after Bitmap recycled
Log.e(TAG, e.getMessage(), e);
}
}
@Override protected void onBeforeClusterRendered(Cluster<MyClusterItem> cluster,
MarkerOptions markerOptions) {
Iterator<MyClusterItem> iterator = cluster.getItems().iterator();
loadAsync(clusterImageView, iterator.next().getImageUrl());
try {
Bitmap icon = clusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
BitmapUtils.recycle(icon);
} catch (Exception e) {
// FIXME workaround : called after Bitmap recycled
Log.e(TAG, e.getMessage(), e);
}
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
return cluster.getSize() > 1;
}
private void loadAsync(final ImageView imageView, final String url) {
// first, get from cache
Bitmap bitmap = thumbCache.get(url);
if (bitmap == null || bitmap.isRecycled()) {
// if there are no cached entry or already recycled, start async load
Picasso.with(getActivity()).load(url).into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
thumbCache.put(url, bitmap);
}
@Override public void onBitmapFailed(Drawable errorDrawable) {
imageView.setImageResource(R.drawable.ic_launcher);
}
@Override public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
} else {
imageView.setImageBitmap(bitmap);
}
}
}