Search code examples
androidgoogle-mapsmarkercolorfilter

set map marker icon with dynamic color in android


How to set custom marker icon with dynamic color that change only green color area with dynamic color

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

//here setting dynamic color to 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;
    }

//its working but it change white color too. How to avoid this and change only green color and keep center white area as it is. Thank you.

enter image description here


Solution

  • I achieve that by following way. I used two separate image (with logo and without logo of same size) the image with logo is transparent and merged those into one to look like a single 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;
        }