意外LayerDrawable行为绘制包含图层时InsetDrawable的 [英] Unexpected LayerDrawable behavior when drawing layers containing InsetDrawable's

查看:200
本文介绍了意外LayerDrawable行为绘制包含图层时InsetDrawable的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在XML建立一个LayerDrawable,其中上层有时会完全覆盖下层。为了使下层小,我使用的是InsetDrawable包装另一个绘制,使其比视图的全尺寸更小。我觉得意外,但是,放在包含了插图也层之上的所有图层应用了插图。我找不到文档支持这种行为,并感到困惑为什么会出现这种情况。

I am trying to build a LayerDrawable in xml where upper layers will occasionally completely obscure lower layers. To make the lower layers smaller, I am using an InsetDrawable to wrap another drawable to make it smaller than the full size of the view. I find unexpectedly, however, that any layers placed on top of the layer containing the inset also has the inset applied to it. I can't find documentation supporting this behavior, and am confused why this would be the case.

在下面的例子中,使一个LayerDrawable 3层。底部和顶部层包含旨在占据整个视图椭圆形绘区域。中间层是一个InsetDrawable内部的矩形抽拉。在code是如下:

In the example below, I make a LayerDrawable with 3 layers. The bottom and top layers contain oval shape drawables that are meant to take up the entire view. The middle layer is a rectangle drawable inside of an InsetDrawable. The code is below:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval" >
            <solid android:color="#00ff00" />
        </shape>
    </item>
    <item>
        <inset
            android:insetBottom="4dp"
            android:insetLeft="4dp"
            android:insetRight="4dp"
            android:insetTop="4dp" >
            <shape android:shape="rectangle" >
                <solid android:color="#ff0000" />
            </shape>
        </inset>
    </item>
    <item>
        <shape android:shape="oval" >
            <solid android:color="#0000ff" />
        </shape>
    </item>

</layer-list>

调用 setBackgroundDrawable(getResources()getDrawable(drawableId)); 在我看来,产生一个绿色椭圆填充整个视图如预期,有一个红色的长方形插图4DP如预期,但蓝色椭圆顶层上也是插页4DP和红色矩形的范围内完全绘制。

Calling setBackgroundDrawable(getResources().getDrawable(drawableId)); in my view produces a green oval that fills the entire view as expected, with a red rectangle inset 4dp as expected, but the blue oval on the top layer is also inset 4dp and drawn completely within the bounds of the red rectangle.

我希望蓝色椭圆遮去绿色椭圆形,大部分红色矩形的,而是它是镶嵌在红色矩形内。有没有什么办法让蓝圈填充视图还保持在上面?

I would expect the blue oval to completely obscure the green oval and most of the red rectangle, but instead it is inset inside the red rectangle. Is there any way to make the blue circle fill the view yet keep it on top?

推荐答案

我也看不出它是记录,但在填补一个 LayerDrawable 是累积的。即,填充在一个层影响所有较高层的边界。这是从<一个href="https://github.com/android/platform_frameworks_base/blob/master/graphics/java/android/graphics/drawable/LayerDrawable.java">the源 LayerDrawable

I also don't see where it is documented, but padding in a LayerDrawable is cumulative. That is, padding at one layer affects the bounds of all higher layers. This is from the source for LayerDrawable:

@Override
protected void onBoundsChange(Rect bounds) {
    final ChildDrawable[] array = mLayerState.mChildren;
    final int N = mLayerState.mNum;
    int padL=0, padT=0, padR=0, padB=0;
    for (int i=0; i<N; i++) {
        final ChildDrawable r = array[i];
        r.mDrawable.setBounds(bounds.left + r.mInsetL + padL,
                              bounds.top + r.mInsetT + padT,
                              bounds.right - r.mInsetR - padR,
                              bounds.bottom - r.mInsetB - padB);
        padL += mPaddingL[i];
        padR += mPaddingR[i];
        padT += mPaddingT[i];
        padB += mPaddingB[i];
    }
}

LayerDrawable.getPadding(矩形)遵循相同的逻辑。)由于 InsetDrawable 使用它的插图作为填充(如<一个href="http://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html#getPadding%28android.graphics.Rect%29">documented),这说明你所看到的行为。

(LayerDrawable.getPadding(Rect) follows the same logic.) Since an InsetDrawable uses its insets as padding (as documented), this explains the behavior you're seeing.

我认为这是一个糟糕的设计决定,但你是那种坚持了下来,我怕。我不认为它可以被覆盖。

I think this is a poor design decision, but you're kind of stuck with it, I'm afraid. I don't think it can be overridden.

这篇关于意外LayerDrawable行为绘制包含图层时InsetDrawable的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆