【问题标题】:Converting a Drawable to a Bitmap to change the color of a Marker in Google Maps Android API v2将 Drawable 转换为位图以更改 Google Maps Android API v2 中标记的颜色
【发布时间】:2013-04-16 12:37:48
【问题描述】:

我正在将旧应用程序从 v1 转换为 v2,但我的标记图标的颜色有问题。我有一个基本的白色图标,需要对其进行着色。

在 v1 中,我是这样做的:

Drawable d = DrawableUtils.resizeImageToDrawable(
    MapViewFragment.mapviewActivity,
    Configuration.Display.getDrawableFix(i),
    Configuration.MapView.getWaypointIconWidth(),
    Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);

overlay = new MyFplnFixListItimizedOverlay(d);

由于 v2 Markers 的图标不接受 Drawable,我考虑将 Drawable 转换为 Bitmap,如下所示:

Drawable d = DrawableUtils.resizeImageToDrawable(
    MapViewFragment.mapviewActivity,
    Configuration.Display.getDrawableFix(i),
    Configuration.MapView.getWaypointIconWidth(),
    Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);

Bitmap icon = ((BitmapDrawable) d).getBitmap();

Marker marker = MapViewFragment.map.addMarker(new MarkerOptions()
    .position(point)
    .title(Integer.toString(fplnType))
    .visible(true)
    .icon(BitmapDescriptorFactory.fromBitmap(icon)));

但由于某种原因,它不起作用。图标保持白色。谁知道为什么?

提前致谢。

【问题讨论】:

    标签: android google-maps drawable google-maps-android-api-2


    【解决方案1】:

    好的,这就是我在一天结束时的做法:

    Drawable d = DrawableUtils.resizeImageToDrawable(
                        MapViewFragment.mapViewActivity,
                        Configuration.Display.getDrawableFix(i),
                        Configuration.MapView.getWaypointIconWidth(),
                        Configuration.MapView.getWaypointIconHeight());
    d.setColorFilter(color, Mode.MULTIPLY);
    
    Bitmap b = ((BitmapDrawable) d).getBitmap();
    Canvas myCanvas = new Canvas(b);
    
    int myColor = b.getPixel(0,0);
    ColorFilter filter = new LightingColorFilter(myColor, color);
    
    Paint pnt = new Paint();
    pnt.setColorFilter(filter);
    myCanvas.drawBitmap(b,0,0,pnt);
    
    Marker marker = MapViewFragment.map.addMarker(new MarkerOptions()
        .position(point)
        .title(Integer.toString(fplnType))
        .visible(true)
        .icon(BitmapDescriptorFactory.fromBitmap(b)));
    

    【讨论】:

    • 感谢分享。你必须处理 OutOfMemoryExceptions 吗?
    【解决方案2】:

    如果你的drawable没有在XML中定义,你可以用这个方法:

    protected Bitmap fromDrawable(final Drawable drawable, final int height, final int width) {
            final int widthDip = (int) TypedValue.applyDimension(1, width, getResources()
                    .getDisplayMetrics());
                final int heightDip = (int) TypedValue.applyDimension(1, width, getResources().getDisplayMetrics());
            final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            final Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }
    

    请注意,如果您需要在Activity 之外进行操作,您还需要传递Context(以便调用context.getResources().getDisplayMetrics())。您还可以传递 DisplayMetrics 对象。

    【讨论】:

      【解决方案3】:

      我不确定这个 DrawableUtils 在做什么,但你正在将它转换为 BitmapDrawable。真的是 BitmapDrawable 吗?

      这里有一些代码可以从任何类型的可绘制对象中提取位图:

      Drawable d = // make sure it's not null
      d.setBounds(0, 0, width, height);
      Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(b);
      d.draw(c);
      

      这个想法是您在可变位图上创建一个画布并在画布上绘制该可绘制对象

      编辑:

      我刚刚看到@Analizer 的答案,是的,如果你没有 Drawable 作为资源,我的答案很好。但如果你这样做,请改用他的答案。

      【讨论】:

      • 我没有将 DrawableUtils 转换为 Drawable,我只是使用它来将 drawable 调整为特定大小。另外,感谢您的建议,但我对“显示”和“配置”应该放什么感到困惑。
      • 对不起,我应该多解释一下,而且还有一个错误(检查更新的代码)。配置可以是此处的任何选项:developer.android.com/reference/android/graphics/… 和显示此处的选项 developer.android.com/reference/android/util/… 显示您可以获得显示指标,我认为它已经将位图预缩放到显示,但你会必须自己测试一下,因为这只是我的猜测。但正如您在编辑中看到的那样,您可以只构建没有显示。
      • 但是接下来我该怎么做呢?我应该直接使用位图作为我的标记图标吗?我试过了,它不起作用,标记没有显示。
      • 我写这段代码已经过去了几个月。我不知道。尝试将此位图放在 ImageView 中。这将测试问题是生成的位图还是您如何将位图设置为标记。
      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 2013-03-22
      • 2013-08-31
      • 2013-08-19
      • 2012-12-29
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多