是否可以在Google Maps API v2中设置自定义的彩色标记?我有一个白色的可绘制资源,我想对其应用颜色过滤器。我已经试过了:
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())));
但这不起作用。它仅显示白色标记,而没有自定义颜色。我传递给setColorFilter()的“ color”字符串的值的形式为“ #RRGGBB”。
我在这里找到答案:https ://groups.google.com/forum/#! topic /android-developers/KLaDMMxSkLs您应用于Drawable的ColorFilter不会直接应用于位图,而是应用于用于渲染位图。因此,修改后的工作代码如下所示:
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);
...现在我们可以将obm添加为彩色地图标记:
map.addMarker(new MarkerOptions().position(eventLocation)
.title(e.getName()).snippet(e.getLocation())
.icon(BitmapDescriptorFactory.fromBitmap(obm)));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句