4

我正在尝试将 aLayerDrawable用于自定义 UI 小部件,并使用与其他图层不同的边界来绘制一个图层,但它似乎不起作用。本质上,我的代码是这样做的:

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

同时,xml 看起来像这样:

<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"/>

所以理想情况下,我会在一个灰色矩形上看到 some_drawable,从后面显示的灰色量取决于边界计算的结果。但我从来没有看到任何灰色层,这似乎是因为它的边界也在以某种方式设置。

编辑

这是期望与预期结果的视觉效果: 在此处输入图像描述

最上面的图像是我想要实现的;灰色对应于第一层,渐变对应于具有预定义的第二层id。实际结果从不显示灰色。

有谁知道是否可以在LayerDrawable不影响其他层的情况下设置一层的边界?

4

2 回答 2

1

我收到了来自 Android Developers Google Group 的一些回复。从本质上讲,没有可靠的方法来做到这一点,LayerDrawable因为它管理所有它的 child 的状态Drawables。从那以后,我采用了一种使用两个单独的可绘制对象的方法,结果令人满意。这是供参考的讨论线程:

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

于 2012-07-31T21:19:56.057 回答
0

我已经设法用这个代码做了类似的事情,使用 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);

结果是(在黑色背景上):

于 2015-11-16T12:22:18.690 回答