Search code examples
androidandroid-widgetdrawablebounds

android set bounds for one layer of layer drawable


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?


Solution

  • 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