是否可以在 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() 的“颜色”字符串的值采用“#RRGGBB”的形式。
最佳答案
我在这里找到了答案:https://groups.google.com/forum/#!topic/android-developers/KLaDMMxSkLs您应用于 Drawable 的 ColorFilter 不会直接应用于 Bitmap,而是应用于用于渲染 Bitmap 的 Paint。所以修改后的工作代码看起来像这样:
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 标记:
map.addMarker(new MarkerOptions().position(eventLocation)
.title(e.getName()).snippet(e.getLocation())
.icon(BitmapDescriptorFactory.fromBitmap(obm)));
关于android - 自定义彩色绘图作为 Google Maps API v2 中的 map 标记 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18868696/