最初に色付けしたい Drawable (白い円) があり、それを ItemizedOverlay のマーカーとして使用します。つまり、同じ Drawable を使用して、マップ上に緑色の円とオレンジ色の円を表示したいと考えています。
ItemizedOverlay.setMarker() を呼び出す前に、ドローアブルで setColorFilter を使用するだけでは機能しないようです。ここで詳しく説明されている変更可能な Bitmap アプローチも試しました - setColorFilter は Android < 2.2 では機能しません。どちらのアプローチも、色を付けずに白い円を描くだけです。
誰かが私を助けることができますか?Android 2.2でこれを実行しています。
ありがとう!
アップデート:
How to change colors of a Drawable in Android?のコードを適合させました。そしてそれを私のために働かせました。
私の方法は次のようになります。
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;
}
ただし、このアプローチはかなりリソースを消費します。これを達成するためのより良い/より高速な/よりスマートな方法があるかどうかを知りたいです。
ありがとう!