I have a Drawable (a white circle) that I would like to colorize first, and then use it as a Marker in an ItemizedOverlay i.e. I'd like to use the same Drawable to show a green circle and an orange circle on the map.
Simply using setColorFilter on the drawable before calling ItemizedOverlay.setMarker() does not seem to work. I've also tried the mutable Bitmap approach detailed here - setColorFilter doesn't work on Android < 2.2. Both approaches simply draw the white circle with no color.
Can someone help me out? I'm running this on Android 2.2.
Thanks!
Update:
I adapted the code from How to change colors of a Drawable in Android? and got it to work for me.
My method looks like this:
private HashMap<Integer, Drawable> coloredPinCache = new HashMap<Integer, Drawable>();
public static final int ORIGINAL_PIN_COLOR = Color.WHITE;
public static final int COLOR_MATCH_THRESHOLD = 100;
protected Drawable getPinInColor(int color) {
// Check if we already have this in the cache
Drawable result = coloredPinCache.get(color);
// If not, create the Drawable
if (result == null) {
// load the original pin
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
// create a mutable version of this
Bitmap mutable = original.copy(Bitmap.Config.ARGB_8888, true);
// garbage-collect the original pin, since it is not needed further
original.recycle();
original = null;
// loop through the entire image
// set the original color to whatever color we want
for(int x = 0;x < mutable.getWidth();x++){
for(int y = 0;y < mutable.getHeight();y++) {
if(match(mutable.getPixel(x, y), ORIGINAL_PIN_COLOR))
mutable.setPixel(x, y, color);
}
}
// create the Drawable from the modified bitmap
result = new BitmapDrawable(mutable);
// cache this drawable against its color
coloredPinCache.put(color, result);
}
// give back the colored drawable
return result;
}
private boolean match(int pixel, int color) {
return Math.abs(Color.red(pixel) - Color.red(color)) < COLOR_MATCH_THRESHOLD &&
Math.abs(Color.green(pixel) - Color.green(color)) < COLOR_MATCH_THRESHOLD &&
Math.abs(Color.blue(pixel) - Color.blue(color)) < COLOR_MATCH_THRESHOLD;
}
However, this approach is pretty resource-intensive. I'd still like to know if there's a better/faster/smarter way to accomplish this.
Thanks!