简体   繁体   中英

Android change Opacity of some items in ItemizedOverlay

I'm trying to change the alpha-channel of some OverlayItems in an ItemizedOverlay . The ItemizedOverlay is added to a MapView in Android.

If I use this code, all items have the same opacity.
According to the logs the condition hits in about 50%...

Any ideas? It doesn't help if I override the getMarker() method in the OverlayItem either. It's the same effect.

This approach does work if I use 2 different drawables (1 for each case)... but I need to use the same drawable.

for (OverlayItem overlay : overlays) {              
    Drawable marker = boundCenterBottom(context.getResources().getDrawable(markerId));
    if (/* some condition - about 50/50 */) {           
        marker.setAlpha(100);
        Log.d("itemized Overlay", "FILTER set marker to => " + marker);
    } else {
        Log.d("itemized Overlay", "NOFILT set marker to => " + marker);
    }

    overlay.setMarker(marker);
}

You have to call mutate() on the Drawable instance, because otherwise it will share the state.

Like said in the documentation of mutate():

[...] A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. [...]

Pretty simple, it works this way:

for (OverlayItem overlay : overlays) {              
    Drawable marker = boundCenterBottom(context.getResources().getDrawable(markerId));
    marker.mutate();
    if (/* some condition */) {           
        marker.setAlpha(100);
    }

    overlay.setMarker(marker);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM