如何使用动态颜色设置自定义标记图标,仅使用动态颜色更改绿色区域
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;
}
//它的工作但它也会改变白色。如何避免这种情况,只改变绿色并保持中心白色区域。谢谢。
答案 0 :(得分:3)
我通过以下方式实现这一目标。我使用了两个单独的图像(带有徽标,没有相同尺寸的徽标)带有徽标的图像是透明的,并将它们合并为一个看起来像一个图像。
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;
}