Custom color drawing as map marker in Google Maps API v2 - Android

Is it possible to set custom color markers in the Google Maps API version 2? I have a white resource and I want to apply a color filter to it. I tried this:

String color = db.getCategoryColor(e.getCategoryId()); Drawable mDrawable = this.getResources().getDrawable(R.drawable.event_location); mDrawable.setColorFilter(Color.parseColor(Model.parseColor(color)),Mode.SRC_ATOP); map.addMarker(new MarkerOptions().position(eventLocation) .title(e.getName()).snippet(e.getLocation()) .icon(BitmapDescriptorFactory.fromBitmap(((BitmapDrawable) mDrawable).getBitmap()))); 

but that will not work. It shows only a white marker without a custom color. The value of the string "color" that I pass to setColorFilter () is of the form "#RRGGBB".

+4
source share
1 answer

Set up the answer here: https://groups.google.com/forum/#!topic/android-developers/KLaDMMxSkLs . The ColorFilter that you apply to the Drawable does not apply directly to the bitmap, it applies to the Paint used to render the Bitmap. Therefore, the modified working code will look like this:

 String color = db.getCategoryColor(e.getCategoryId()); Bitmap ob = BitmapFactory.decodeResource(this.getResources(),R.drawable.event_location); Bitmap obm = Bitmap.createBitmap(ob.getWidth(), ob.getHeight(), ob.getConfig()); Canvas canvas = new Canvas(obm); Paint paint = new Paint(); paint.setColorFilter(new PorterDuffColorFilter(Color.parseColor(Model.parseColor(color)),PorterDuff.Mode.SRC_ATOP)); canvas.drawBitmap(ob, 0f, 0f, paint); 

... and now we can add obm as a color map marker:

 map.addMarker(new MarkerOptions().position(eventLocation) .title(e.getName()).snippet(e.getLocation()) .icon(BitmapDescriptorFactory.fromBitmap(obm))); 
+13
source

Source: https://habr.com/ru/post/1502772/


All Articles