Android LayerDrawable.setDrawableByLayerId not working on HTC Sensation (and others?)

I use LayerDrawable to create a series of hot spots (indexes 1+) on the base image (index 0). Hot spots are added based on user interface interaction, and their position is dynamic, so I do all this programmatically, rather than using XML. As an additional (probably irrelevant) detail, I use a snippet to hold LayerDrawable, as this is in the context of the FragmentStatePagerAdapter and ViewPager.

The problem is that when I try to update the image by changing Drawable to LayerDrawable using setDrawableByLayerId, the image will not change (even though the method returns true, indicating that it found this layer).

How can I make this work?

+2
source share
3 answers

Answering my own question here, in the hope that it is useful to someone there. The only way to get this to work on my HTC Sensation is to recreate LayerDrawable from scratch, and also change:

LayerDrawable oldLayerDrawable = (LayerDrawable) imageView.getDrawable();
Drawable[] layers = new Drawable[2];
layers[0] = oldLayerDrawable.getDrawable(0);
layers[1] = getResources().getDrawable(R.drawable.hotspot_dot);
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);

This is not very, but it seems that this may be a known issue.

+3
source
newdrawable.setBounds(mLayerDrawable.findDrawableByLayerId(R.id.image_source).getBounds());
        mLayerDrawable.setDrawableByLayerId(R.id.image_source, drawable);        
        mLayerDrawable.invalidateSelf();

link: https://github.com/ytRino/LayerDrawableSample/blob/master/src/net/nessness/android/sample/layerdrawable/LayerDrawableSampleActivity.java

+1
source

, "index" "id" - . layerId -1. , setId(index, id), setDrawableByLayerId(id, Drawable)

0
source

All Articles