Setting map marker icons with dynamic colors in Android

How to set a custom marker icon with dynamic color, which can only change the green area with dynamic color

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

//Here, the dynamic color is set as the marker icon

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

// it works, but it also changes white. How to avoid this situation, only change green and keep the central white area unchanged. Thank you

resolvent:

I achieved this by using two separate images (with logo and without logo of the same size), the image with logo is transparent, and merging them into one image

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>