Android settings for one layer layer

I am trying to use LayerDrawable for a user interface widget and have one drawing layer with different borders than other layers, but it does not seem to work. Essentially, my code does this:

 int left, top, right, bottom; /*... do some math ... */ Drawable d = mDrawable.findDrawableByLayerId(R.id.some_specific_layer); d.setBounds(left, top, right, bottom); 

Meanwhile, xml looks like this:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <solid android:color="#ff999999" /> </shape> </item> <item android:id="@id/some_specific_layer" android:drawable="@drawable/some_drawable"/> 

Ideally, I would see some_drawable above the gray rectangle, with the amount of gray shown at the back, depending on the result of the calculation of the borders. But I never see any of the gray layer, and it seems because its borders are also set in some way.

EDIT

Here is the desired and expected result: enter image description here

The top image is what I want to achieve; gray corresponds to the first layer, and the gradient corresponds to the second layer with a predefined id . The actual result never shows gray.

Does anyone know if it is possible to set the borders of one LayerDrawable layer without affecting other layers?

+4
source share
2 answers

I received answers from the Google Android Developer Team. There is essentially no reliable way to do this with LayerDrawable , as it manages state for all of its child Drawables . Since then, I have adopted an approach that instead uses two separate graphs, with satisfactory results. Here is a discussion thread for reference:

https://groups.google.com/d/topic/android-developers/fUxtJstdkBM/discussion

+1
source

I managed to do a similar thing with this code using LayerDrawable:

 // create first shape ShapeDrawable shape1 = new ShapeDrawable(new OvalShape()); shape1.getPaint().setColor(0xFFFFFF00); shape1.setIntrinsicWidth(50); shape1.setIntrinsicHeight(50); shape1.invalidateSelf(); // create second shape ShapeDrawable shape2 = new ShapeDrawable(new OvalShape()); shape2.getPaint().setColor(0xFFFFB400); shape2.setIntrinsicWidth(20); shape2.setIntrinsicHeight(50); shape2.invalidateSelf(); // create layerDrawable (layer-list in xml) // it containt our 2 shapes and the order in wich they will be drawn (shape2 is on top). LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{shape1, shape2}); // ajust position shape1 and shape2 layerDrawable.setLayerInset(0, 0,0,0,0); layerDrawable.setLayerInset(1, 15,0,15,0); 

Result (on a black background):

0
source

All Articles