【问题标题】:Custom coloured drawable as map marker in Google Maps API v2 - AndroidGoogle Maps API v2 中的自定义彩色可绘制地图标记 - Android
【发布时间】:2013-09-23 00:05:57
【问题描述】:

是否可以在 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”的形式。

【问题讨论】:

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


    【解决方案1】:

    我在这里找到了答案:https://groups.google.com/forum/#!topic/android-developers/KLaDMMxSkLs 应用于 Drawable 的 ColorFilter 不会直接应用于位图,而是应用于用于渲染位图的 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.addMarker(new MarkerOptions().position(eventLocation)
        .title(e.getName()).snippet(e.getLocation())
        .icon(BitmapDescriptorFactory.fromBitmap(obm)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2013-03-30
      • 2013-08-26
      • 2013-03-22
      • 1970-01-01
      相关资源
      最近更新 更多