Android : Background gradient on map marker custom drawable doesn't show up correctly on Android 4

on Friday, March 27, 2015


I'm creating custom map marker icons that include an image and text. The text has a map_marker_glow.png background, which is an image that provides an oval-shaped white gradient behind the text so that it's still readable on the map.


It works great on Android 5:


enter image description here


However on Android 4 the gradient is showing up with a gray oval around it. Here is an image from 4.3:


enter image description here


Any ideas on why this is happening, and any code / image changes I can make to resolve it?


Here is MapMarkerIcon.axml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/map_marker_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/map_marker_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/black"
android:textSize="@dimen/text_size_count"
android:textStyle="bold"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/map_marker_glow" />
</LinearLayout>


And here is the code that creates a bitmap out of the layout, which can then be set as the map marker:



private Bitmap CreateIcon (Drawable drawable, string text) {
EnsureInit ();

// Update the icon
iconView.SetImageDrawable (drawable);
drawable.Dispose ();

// Update the text
textView.Text = text;
textView.Visibility = !String.IsNullOrEmpty(text) ? ViewStates.Visible : ViewStates.Gone;
if (text == " ")
textView.SetBackgroundColor (Color.Transparent);

// Lay out size and measure
var measureSpec = ViewGroup.MeasureSpec.MakeMeasureSpec (0, MeasureSpecMode.Unspecified);
container.Measure (measureSpec, measureSpec);
int measuredWidth = container.MeasuredWidth;
int measuredHeight = container.MeasuredHeight;
container.Layout (0, 0, measuredWidth, measuredHeight);

// Take current layout and draw to a bitmap
var bitmap = Bitmap.CreateBitmap (measuredWidth, measuredHeight, Bitmap.Config.Argb4444); // According to API docs, this is deprecated to Argb8888; however, testing shows a reduction in memory pressure
bitmap.EraseColor (Color.Transparent);
using (var canvas = new Canvas (bitmap)) {
container.Draw (canvas);
}
return bitmap;
}

private void EnsureInit () {
if (container == null) {
container = ApplicationContext.Activity.LayoutInflater.Inflate (Resource.Layout.MapMarkerIcon, null) as ViewGroup;
iconView = container.FindViewById<ImageView> (Resource.Id.map_marker_icon);
textView = container.FindViewById<TextView> (Resource.Id.map_marker_text);
}
}

0 comments:

Post a Comment