着色Google Maps Android API标记

时间:2016-04-25 19:12:25

标签: android google-maps google-maps-markers android-canvas google-maps-android-api-2

我正在尝试将标记的颜色设置为colorAccent但不知何故它不适用于此代码:

Drawable drawable = getResources().getDrawable(R.drawable.ic_place_white_24dp);
drawable.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(((BitmapDrawable) drawable).getBitmap());
Bitmap workingBitmap = Bitmap.createBitmap(((BitmapDrawable) drawable).getBitmap());
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
drawable.draw(canvas);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(mutableBitmap);
markerOptions.icon(bitmapDescriptor);

我尝试将drawable从纯白色图标切换为黑色图标,并尝试从Mode.MULTIPLY切换到Mode.ADD。两者都没有成功。

1 个答案:

答案 0 :(得分:2)

尝试这个!!!!

private static Paint markerPaint;
private static Paint whitePaint;

Bitmap markerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mapmarker);
Bitmap resultBitmap = Bitmap.createBitmap(markerBitmap, 0, 0, markerBitmap.getWidth() - 1, markerBitmap.getHeight() - 1);
ColorFilter filter = new PorterDuffColorFilter(Themer.getPrimaryColor(getActivity()), PorterDuff.Mode.SRC_IN);
if (markerPaint == null) {
    markerPaint = new Paint();
    markerPaint.setColorFilter(filter);
} else {
    markerPaint.setColorFilter(filter);
}
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, markerPaint);
if (Themer.shouldShowStopCounts(getActivity())) {
    if (whitePaint == null) {
        whitePaint = new Paint();
        whitePaint.setColor(Color.WHITE);
        whitePaint.setTextSize(40f);
        whitePaint.setTextAlign(Paint.Align.CENTER);
    }
    canvas.drawText(item.name, resultBitmap.getWidth() / 2, resultBitmap.getHeight() / 2, whitePaint);
}
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(resultBitmap));