Home Force Android canvas to be on top of bitmap used for Google Maps markers
Reply: 1

Force Android canvas to be on top of bitmap used for Google Maps markers

user1202032 Published in 2017-11-14 22:56:03Z

I'm drawing a circle with some text on top of a bitmap, and then using the bitmap as the icon for a Google Maps marker/pin.

Most of the time, it works perfectly, but sometimes the circle+text will show up BEHIND the bitmap - not good. It seems to consistently happen if i navigate to a new activity, and then go back.


// Make room for the circle
Bitmap myBitmap = Bitmap.createBitmap(dstWidth, dstHeight, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(myBitmap);
Matrix matrix = new Matrix();
int xMatrix = xPadding / 2;
matrix.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(xMatrix,yPadding, dstWidth - xMatrix, dstHeight), Matrix.ScaleToFit.CENTER);
canvas.drawBitmap(bitmap, matrix, null);
canvas.drawCircle(xPos, yPos, 30, backgroundPaint);
if(_badge != null && _badge.equalsIgnoreCase("") == false) {
    canvas.drawText(_badge, xPos, yPos+10, textPaint);
return myBitmap;

Is there a way i can force my circle+text to be on top? Perhaps i should try to make my bitmap immutable (how do i do this?)

Ibrahim Reply to 2017-11-14 23:20:16Z

Identifying the issue: It's seems that you "don't clean after finishing your work".

What is that mean?: You are correctly showing circle and text on the top.

But after close activity/fragment and reopen the map you get weird result because of drawing new canvas above last one. And you're doing that every time you open the map so it will be more weird of you navigated from map fragment much times.

Why that happened?:

Having multiple views (old canvas + old text and circle plus news of them) do that.


Clear what you have made!.

One of the easiest ways is to clear your map in onStop() or onDestroy() : mMap.clear().

Other way: Is to draw canvas and objects only if needed.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.345297 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO