如何用动态颜色设置自定义标记图标,动态颜色只改变绿色区域
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;
}