As one can see in the image above, the custom map markers from the Iconify library that I create on a SupportMapFragment are transparent. How can I make these markers opaque?
Here is how I draw the markers on the map:
IconDrawable icon = new IconDrawable(getActivity(), Iconify.IconValue.fa_map_marker)
.colorRes(R.color.birthEvent)
.sizeDp(40);
icon.setAlpha(255);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
icon.draw(canvas);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
MarkerOptions marker = new MarkerOptions()
.icon(bitmapDescriptor);
googleMap.addMarker(marker);
Here is how the color birthEvent is defined in res/values/colors.xml:
<resources>
<color name="birthEvent">#FD3C3C</color>
</resources>
The
markerOptions.alpha()
method should be specified as a float between 0.0 and 1.0, where 0 is
fully transparent and 1 is fully opaque. So try to change it as a
float between 0 to 1.
Or you can check this blog on how to set the Android iconify in MapFragment.
Here is the example code.
public BitmapDescriptor getCustomMarker(IconValue iconValue) {
IconDrawable id = new IconDrawable(getBaseContext(), iconValue) {
#Override
public void draw(Canvas canvas) {
// The TextPaint is defined in the constructor
// but we override it here
TextPaint paint = new TextPaint();
paint.setTypeface(Iconify.getTypeface(getBaseContext()));
paint.setStyle(Paint.Style.STROKE_AND_FILL);
paint.setTextAlign(Paint.Align.CENTER);
paint.setUnderlineText(false);
// If you need a custom color specify it here
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(getBounds().height());
Rect textBounds = new Rect();
String textValue = String.valueOf(iconValue.character());
paint.getTextBounds(textValue, 0, 1, textBounds);
float textBottom = (getBounds().height() - textBounds.height()) / 2f + textBounds.height() - textBounds.bottom;
canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
}
}.actionBarSize();
Drawable d = id.getCurrent();
Bitmap bm = Bitmap.createBitmap(id.getIntrinsicWidth(), id.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
d.draw(c);
return BitmapDescriptorFactory.fromBitmap(bm);
}
Related
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?
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));
I'm trying to set a marker's color to colorAccent but somehow it doesn't work with this code:
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);
I tried to switch the drawable from a plain white icon to a black one and also tried to switch from Mode.MULTIPLY to Mode.ADD. Both without success.
TRY THIS!!!!
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));
I have been trying to fill the polygon I am drawing in a Goole Map but
I can't get it right. In my code below, when I just use drawPaint, it draws in the right location but when I use drawPath, It is painting the bitmap in different locations.
Bitmap fillBMP = PatternGenerator.makePattern(PatternGenerator.PATTERN_STYLE_X, lineColor);
BitmapShader fillBMPshader = new BitmapShader(fillBMP, BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT);
Paint paintshader = new Paint();
paintshader.setAntiAlias(true);
paintshader.setShader(fillBMPshader);
android.graphics.Point r1 = projection.toScreenLocation(mLatLngBounds.northeast);
android.graphics.Point r2 = projection.toScreenLocation(mLatLngBounds.southwest);
int width = Math.abs(Math.abs(r1.x) - Math.abs(r2.x));
int height = Math.abs(Math.abs(r2.y) - Math.abs(r1.y)); //Math.abs(r2.y - r1.y);
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint p = new Paint();
p.setColor(Color.YELLOW);
//canvas.drawPaint(p); // This works
canvas.drawPath(path, paintshader); // This does not work.
GroundOverlay overlay = map.addGroundOverlay(new GroundOverlayOptions()
.anchor(0f, 0f)
.image(BitmapDescriptorFactory.fromBitmap(bm))
.transparency(0.3f)
.positionFromBounds(mLatLngBounds));
The Path I created is a series of points.
-23.725012,137.285156
-24.527135,145.371094
-29.382175,145.898438
-29.993002,138.691406
-23.725012,137.285156
int nPoints = mOuter.mCoordinates.size();
android.graphics.Point pos0 = projection.toScreenLocation(mOuter.mCoordinates.get(0));
path.moveTo(pos0.x, pos0.y);
for ( int i = 1; i < nPoints; i++)
{
builder.include(mOuter.mCoordinates.get(i));
android.graphics.Point pos = projection.toScreenLocation(mOuter.mCoordinates.get(i));
path.lineTo(pos.x, pos.y);
}
path.close();
I used moveTo then lineTo and close the path.
Is there something that I am missing here?
I have fixed the problem by getting the bounding rect of the Path object.
And using the coordinates of the RectF to draw the bitmap
and use the .left and .top of RectF to offset the starting position
of the path.
// This will get the bounding rect of the path and use
// the width and height of the rect to create the Bitmap.
RectF rectF = new RectF();
path.computeBounds(rectF, true);
int width = (int)rectF.width();
int height = (int)rectF.height();
Bitmap bmPattern = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmPattern);
// Need to Offset the origin so that it will drawn properly
canvas.translate(rectF.left * -1, rectF.top * -1);
canvas.drawPath(path, paintshader);
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.
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;
}
for an Android App I need a custom marker for my google maps activity. The standard option do not help me. what is the best way to achieve the right icon with a businesslogo that can be set for each marker?
UPDATE:
Sorry, either I wasn't clear enough or I don't see it. I can't find a lot helpful things in the docs or the hints you gave me. Now I constructed a default marker:
and I have lots of profile pics or logos wich need to be placed within the marker at runtime depending on certain conditions e.g.:
There is a documentation about adding custom markers on a position.
private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
private Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
for making custom view you have to use MarkerDemoActivity class to use a custom marker.if you are using google map Api V2.0.
and other solution to make custom view for marker.
Add this code to add marker of map:
Marker myLocMarker = map.addMarker(new MarkerOptions()
.position(myLocation)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
the writeTextOnDrawable() Method:
private Bitmap writeTextOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(convertToPixels(context, 11));
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//If the text is bigger than the canvas , reduce the font size
if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(context, 7)); //Scaling needs to be used for different dpi's
//Calculate the positions
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, xPos, yPos, paint);
return bm;
}
public static int convertToPixels(Context context, int nDP)
{
final float conversionScale = context.getResources().getDisplayMetrics().density;
return (int) ((nDP * conversionScale) + 0.5f) ;
}
and for more information see this link.
So this is how i did it firt i constructed two pics. the marker as shown above and the other one by the following function (all code is found soewhere on stackoverflow):
public static Bitmap getCircleBitmap(Bitmap bm) {
int sice = Math.min((bm.getWidth()), (bm.getHeight()));
Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, sice, sice);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffff0000;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 4);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Then I pass them to the following function
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 5, 5, null);
return bmOverlay;
}
I know its probably not the best way of doing it, especially i dont like the hard coding of the position of the circle in the marker. but t works so far