Convert Drawable to Bitmap to change marker color in Google Maps API Android v2

I am converting an old application from v1 to v2 and I have a problem with the color of my Marker icons. I have a basic white icon and need to be colored.

In v1, I did it as follows:

Drawable d = DrawableUtils.resizeImageToDrawable( MapViewFragment.mapviewActivity, Configuration.Display.getDrawableFix(i), Configuration.MapView.getWaypointIconWidth(), Configuration.MapView.getWaypointIconHeight()); d.setColorFilter(color, Mode.MULTIPLY); overlay = new MyFplnFixListItimizedOverlay(d); 

Since v2 markers do not accept Drawables for their icons, I was thinking of converting Drawable to Bitmap, for example:

 Drawable d = DrawableUtils.resizeImageToDrawable( MapViewFragment.mapviewActivity, Configuration.Display.getDrawableFix(i), Configuration.MapView.getWaypointIconWidth(), Configuration.MapView.getWaypointIconHeight()); d.setColorFilter(color, Mode.MULTIPLY); Bitmap icon = ((BitmapDrawable) d).getBitmap(); Marker marker = MapViewFragment.map.addMarker(new MarkerOptions() .position(point) .title(Integer.toString(fplnType)) .visible(true) .icon(BitmapDescriptorFactory.fromBitmap(icon))); 

But for some reason it does not work. The icons remain white. Does anyone know why?

Thanks in advance.

+6
source share
3 answers

OK, here's how I did it at the end of the day:

 Drawable d = DrawableUtils.resizeImageToDrawable( MapViewFragment.mapViewActivity, Configuration.Display.getDrawableFix(i), Configuration.MapView.getWaypointIconWidth(), Configuration.MapView.getWaypointIconHeight()); d.setColorFilter(color, Mode.MULTIPLY); Bitmap b = ((BitmapDrawable) d).getBitmap(); Canvas myCanvas = new Canvas(b); int myColor = b.getPixel(0,0); ColorFilter filter = new LightingColorFilter(myColor, color); Paint pnt = new Paint(); pnt.setColorFilter(filter); myCanvas.drawBitmap(b,0,0,pnt); Marker marker = MapViewFragment.map.addMarker(new MarkerOptions() .position(point) .title(Integer.toString(fplnType)) .visible(true) .icon(BitmapDescriptorFactory.fromBitmap(b))); 
+6
source

If your drawable is not defined in XML, you can do this with this method:

 protected Bitmap fromDrawable(final Drawable drawable, final int height, final int width) { final int widthDip = (int) TypedValue.applyDimension(1, width, getResources() .getDisplayMetrics()); final int heightDip = (int) TypedValue.applyDimension(1, width, getResources().getDisplayMetrics()); final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

Note that if you need to do this from OUTSIDE a Activity , you also need to pass Context (to call context.getResources().getDisplayMetrics() ). You also pass a DisplayMetrics object.

+1
source

I'm not sure what this DrawableUtils does, but you drop it in BitmapDrawable. Is it Really BitmapDrawable?

here is some code to extract any type of bitmap:

 Drawable d = // make sure it not null d.setBounds(0, 0, width, height); Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); d.draw(c); 

The idea is that you create a canvas on a mutable bitmap and draw it on a canvas

change

I just saw the @Analizer answer and yes, my answer is good if you do not have a Drawable resource as a resource. But if you do, ask him to answer.

0
source

All Articles