Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using LayerDrawable to build up a series of hotspots (indexes 1+) on an underlying image(index 0). The hotspots are added based on user interface interaction, and their position is dynamic, so I'm doing all of this programatically rather than using XML. As a further (probably irrelevant) detail, I am using a Fragment to hold the LayerDrawable, as this is in the contexts of an FragmentStatePagerAdapter and ViewPager.

The problem is that when I try to update an image by changing the Drawable in the LayerDrawable by called setDrawableByLayerId, the image does not change (despite the method returning true, indicating that it found the layer).

How can I get this to work?

like image 762
Brendan Avatar asked Nov 17 '25 10:11

Brendan


2 Answers

Answering my own question here, in the hope that it's useful to somebody out there. The only way I could get this to work on my HTC Sensation was by recreating the LayerDrawable from scratch, plus the 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);

It's not pretty, but it seems that this might be a known problem.

like image 123
Brendan Avatar answered Nov 19 '25 23:11

Brendan


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

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

like image 28
QVDev Avatar answered Nov 20 '25 01:11

QVDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!