4

I'm trying to use a LayerDrawable for a custom UI widget and have one layer be drawn with different bounds than the other layers, but it doesn't seem to be working. 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, the 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"/>

So ideally I would see some_drawable over a gray rectangle, with the amount of gray showing from behind depending on the result of the bounds computation. But I never see any of the gray layer, and it appears to be because it's bounds also are being set somehow.

EDIT

Here's a visual of desired versus expected result: enter image description here

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

Does anyone know if it's possible to set the bounds of one layer of a LayerDrawable without affecting other layers?

Karakuri
  • 38,365
  • 12
  • 84
  • 104

2 Answers2

1

I got some responses from the Android Developers Google Group. Essentially, there's no reliable way to do this with LayerDrawable since it manages the state for all it's child Drawables. I've since adopted an approach that uses two separate drawables instead, with satisfactory results. Here's the discussion thread for reference:

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

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Please add code sample or describe solution for the problem, since this url and discussion with solution are not accessible – Gordon Freeman May 25 '17 at 11:11
  • Unfortunately I don't recall the details of the discussion, nor the solution I ended up with. I probably just used a background image or drew the drawables myself on a Canvas. – Karakuri Jun 12 '17 at 16:04
0

I've managed to do a similar thing with this code, using a 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);

The result is (on a black background):

Maxime Ancelin
  • 870
  • 9
  • 11