在 android 中设置具有动态颜色的地图标记图标

set map marker icon with dynamic color in android

如何设置动态颜色的自定义标记图标,只改变动态颜色的绿色区域

googleMap.addMarker(new MarkerOptions()
                                .position(latLng)
                                .visible(true)
                          .icon(BitmapDescriptorFactory.fromBitmap(changeBitmapColor(color))));

//此处为标记图标设置动态颜色

private Bitmap changeBitmapColor(int color) {
        Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_def);
        Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
                sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);

        Paint p = new Paint();
        ColorFilter filter = new LightingColorFilter(color, 0);
        p.setColorFilter(filter);

        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawBitmap(resultBitmap, 0, 0, p);


        return  resultBitmap;
    }

//它的工作,但它也改变了白色。如何避免这种情况并仅更改绿色并保持中心白色区域不变。谢谢。

我是通过以下方式实现的。我使用了两个单独的图像(带徽标和不带徽标的相同大小)带徽标的图像是透明的,并将它们合并为一个看起来像一个图像。

googleMap.addMarker(new MarkerOptions()
                                        .position(latLng)
                                        .visible(true).snippet(String.valueOf(i))
                                        .icon(BitmapDescriptorFactory.fromBitmap(changeBitmapColor(color))));



private Bitmap changeBitmapColor(int color) {

        Bitmap ob = BitmapFactory.decodeResource(this.getResources(), R.drawable.pin_fill);
        Bitmap obm = Bitmap.createBitmap(ob.getWidth(), ob.getHeight(), Bitmap.Config.ARGB_8888);
        Bitmap overlay = BitmapFactory.decodeResource(this.getResources(), R.drawable.pin_trans);
        Bitmap overlaym = Bitmap.createBitmap(overlay.getWidth(), overlay.getHeight(), Bitmap.Config.ARGB_8888);


        Canvas canvas = new Canvas(overlaym);
        Paint paint = new Paint();
        paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        canvas.drawBitmap(ob, 0f, 0f, paint);
        canvas.drawBitmap(overlay, 0f, 0f, null);
        return overlaym;
    }