android - Andorid Google map show custom marker and clustering issue -
i show custom marker google map , cluster them. marker contains imageview
shows avatar downloaded network. here target:
everythings ok, however, when implemented google maps android marker clustering utility, imageview
shows same avatar (sometime 2 wrong avatars).
here custom markerrender:
public class markerrender extends defaultclusterrenderer<image> { private static final string tag = markerrender.class.getsimplename(); private icongenerator clustergenerator; private icongenerator markergenerator; private imageview mimgmarkerthumbnail; private imageview mimgmarkerclusterthumbnail; private textview txtsizecluster; private activity activity; private bitmap mask, background; private atomicinteger imagedownloadcounter; private int totalitem; private imagesize imagesize; public markerrender(fragmentactivity activity, googlemap mmap, clustermanager<image> mclustermanager) { super(activity, mmap, mclustermanager); this.activity = activity; imagedownloadcounter = new atomicinteger(0); mask = bitmapfactory.decoderesource(activity.getresources(), r.drawable.annotation_behind); background = bitmapfactory.decoderesource(activity.getresources(), r.drawable.annotation_behind); setupclustericon(); setupmarker(); } private void setupclustericon() { clustergenerator = new icongenerator(activity); view clusterview = activity.getlayoutinflater().inflate(r.layout.custom_marker_cluster, null); txtsizecluster = (textview) clusterview.findviewbyid(r.id.tv_number_marker); mimgmarkerclusterthumbnail = (imageview) clusterview.findviewbyid(r.id.img_load); clustergenerator.setcontentview(clusterview); clustergenerator.setbackground(null); } private void setupmarker() { markergenerator = new icongenerator(activity); view markerview = activity.getlayoutinflater().inflate(r.layout.custom_marker, null); mimgmarkerthumbnail = (imageview) markerview.findviewbyid(r.id.img_load); markergenerator.setcontentview(markerview); markergenerator.setbackground(null); } @override protected void onbeforeclusteritemrendered(final image image, final markeroptions markeroptions) { initimagesizeifneed(); bitmap icon = markergenerator.makeicon(); pflogmanager.instance.loge(tag, "maken icon: " + icon.hashcode()); markeroptions.icon(bitmapdescriptorfactory.frombitmap(icon)); icon.recycle(); } @override protected void onclusteritemrendered(final image image, marker marker) { super.onclusteritemrendered(image, marker); imageloader.getinstance().loadimage(image.getmapimagelink(), imagesize, new simpleimageloadinglistener() { @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { bitmap croppedbitmap = helpers.makecroppedbitmap(loadedimage, background, mask); mimgmarkerthumbnail.setimagebitmap(croppedbitmap); } }); } @override protected void onbeforeclusterrendered(cluster<image> cluster, markeroptions markeroptions) { initimagesizeifneed(); bitmap icon = clustergenerator.makeicon(); markeroptions.icon(bitmapdescriptorfactory.frombitmap(icon)); icon.recycle(); } @override protected void onclusterrendered(cluster<image> cluster, marker marker) { super.onclusterrendered(cluster, marker); arraylist<image> list = new arraylist<>(cluster.getitems()); settextnumbermarker(cluster); string urlfirstimage = list.get(0).getmapimagelink(); imageloader.getinstance().loadimage(urlfirstimage, imagesize, new simpleimageloadinglistener() { @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { final bitmap croppedbitmap = helpers.makecroppedbitmap(loadedimage, background, mask); mimgmarkerclusterthumbnail.setimagebitmap(croppedbitmap); } }); } private void loadclusterthumbnail(string url) { } private void settextnumbermarker(cluster<image> cluster) { int size = cluster.getsize(); if (size > 99) { txtsizecluster.settext("99+"); } else { txtsizecluster.settext(string.valueof(cluster.getsize())); } } @override protected boolean shouldrenderascluster(cluster cluster) { return cluster.getsize() > 1; } }
i've guess issue use 1 imageview
show avatar, try use unique imageview
each marker (by inflat new 1 xml every time needed), result show blank marker (just background , there no avatar).
took 3 days of research without results, please me!
i've resolved myself. solution use marker.seticon()
method after image downloaded netword or got cache. dont use imageview anymore.
so, modified above markerrender
class:
the setupclustericon()
method:
private void setupclustericon() { relativelayout.layoutparams params = new relativelayout.layoutparams(markerwidth, markerheight); imageview marker = new imageview(activity); marker.setlayoutparams(params); clustergenerator = new icongenerator(activity); clustergenerator.setcontentview(marker); clustergenerator.setbackground(null); }
and onclusteritemrendered()
method:
protected void onclusteritemrendered(final image image, final marker marker) { super.onclusteritemrendered(image, marker); imageloader.getinstance().loadimage(image.getmapimagelink(), imagesize, new simpleimageloadinglistener() { @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { bitmap croppedbitmap = helpers.makeclusteritembitmap(background, loadedimage, mask); try { marker.seticon(bitmapdescriptorfactory.frombitmap(croppedbitmap)); } catch (exception e) { e.printstacktrace(); } } }); }
makeclusteritembitmap
helper method:
public static bitmap makeclusteritembitmap(bitmap background, bitmap original, bitmap mask) { bitmap croppedoriginal = makecroppedbitmap(original, mask); bitmap result = bitmap.createbitmap(background.getwidth(), background.getheight(), bitmap.config.argb_8888); canvas mcanvas = new canvas(result); croppedoriginal = bitmap.createscaledbitmap(croppedoriginal, croppedoriginal.getwidth() - 20, croppedoriginal.getheight() - 20, true); mcanvas.drawbitmap(background, 0, 0, null); mcanvas.drawbitmap(croppedoriginal, 10, 10, null); return result; } public static bitmap makecroppedbitmap(bitmap original, bitmap mask) { original = bitmap.createscaledbitmap(original, mask.getwidth(), mask.getheight(), true); bitmap result = bitmap.createbitmap(original.getwidth(), original.getheight(), bitmap.config.argb_8888); canvas mcanvas = new canvas(result); paint paint = new paint(paint.anti_alias_flag); paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in)); mcanvas.drawbitmap(original, 0, 0, null); mcanvas.drawbitmap(mask, 0, 0, paint); paint.setxfermode(null); return result; }
done, finish 3 nightmare researchingdays days :p
however, approach leads new issue: the performance
. cause drawing new bitmap many layers, map laggy bit. i'm thinking in improving :)
any sugguestion appriciate :d
Comments
Post a Comment