android - 自定义彩色绘图作为 Google Maps API v2 中的 map 标记 - Android

标签 android google-maps google-maps-markers android-maps-v2 android-drawable

是否可以在 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/

相关文章:

java - 添加图像作为 TextView 的文本背景

android - 如何使用工具栏在底部显示操作项

java - 安卓录像错误

安卓 : Google Maps API V2 displays black screen ECLIPSE

google-maps - 谷歌地图 : Traffic on top of Custom Map

google-maps - Google Maps API 可以计算沿多个位置或旅程段的路线吗?

android - 如何使用群集标记在谷歌地图标记 A-Z 中添加字母

android - 如何在 AndroidManifest.xml 中添加一个钩子(Hook)以便 Firebase 进行初始化?

javascript - 有没有办法确定点击了哪个谷歌地图标记?

javascript - 声明标记 Javascript Google Maps API