【问题标题】:Tint a Google Maps Android API Marker为 Google Maps Android API 标记着色
【发布时间】:2016-08-19 08:25:44
【问题描述】:

我正在尝试将标记的颜色设置为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);

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

【问题讨论】:

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


    【解决方案1】:

    试试这个!!!!

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多