Search code examples
androidgoogle-mapsgoogle-maps-api-3android-bitmapbitmapfactory

android custom markers with a transparent Bus image


I am trying to add a transparent bus marker to my Google MAP, But it doesn't add the way i wanted as show in pic,GoodOne

image that i use for marker

But this what i get thisiswhatiget

Also it doesn't take bus exact shape but it draws in Rectangle format

here is my code from main activity

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
               Bitmap circle = getCircleBitmap(image, 80);
               Marker mMarker = googleMap.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromBitmap(circle))
                    .flat(true).anchor(0.25f, 0.25f)
                    .visible(true)
                    .position(position));

 public static Bitmap getCircleBitmap(Bitmap bmp, int size){
      Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bmp, 50, 50);
      Bitmap output = Bitmap.createBitmap(size, size,     Bitmap.Config.ARGB_8888);
     Canvas canvas = new Canvas(output);

    int color = Color.RED;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, size, size);
    RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(thumbnail, 15,15, paint);

    //thumbnail.recycle();
    return output;
  }

Please do help on this, Is it that BITMAP makes picture loose transparency?


Solution

  • If your drawable (bus.png for example) has a transparent background (this is very important and according to your result I think that it has a white background), you just need to change the PorterDuff mode to be SRC_OVER in your getCircleBitmap:

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));