为什么Android图层列表可绘制如此智能?

use*_*289 3 android drawable nine-patch android-layout android-5.0-lollipop

我无法深入了解android如何实现其layer-list drawable.但我发现它很有趣,我几乎不知道为什么会这样.

这里有一些drawables:

九补丁xml

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/cam2tst_ripple_bg_img">
</nine-patch>
Run Code Online (Sandbox Code Playgroud)

形状xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding
    android:left="@dimen/cam2tst_ripple_horizontal_padding"
    android:top="@dimen/cam2tst_ripple_vertical_padding"
    android:right="@dimen/cam2tst_ripple_horizontal_padding"
    android:bottom="@dimen/cam2tst_ripple_vertical_padding" />
<solid android:color="@android:color/holo_green_dark" />
</shape>
Run Code Online (Sandbox Code Playgroud)

波纹xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_green_light">
<item android:drawable="@drawable/cam2tst_ripple_shape"></item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

包含以上所有内容的图层列表

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cam2tst_ripple_bg_img" />
<item android:drawable="@drawable/cam2tst_ripple_base" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)

不幸的是,我仍然无法在我的L预览中获得截图,但我可以对其进行描述.

我得到的是形状(我显然没有明确地设置它的大小)并没有覆盖整个九个补丁!九个补丁的未拉伸部分神奇地被认为是某种"自动填充物" ".我期待的是什么(好吧,我正在期待android为我做了什么,我的意思是我......假设...)是不是那么积极的东西:不是特别大小的形状可绘制覆盖整个九-patch就像后者是正常的png一样.

但是这个形状确实神奇地避免了9个补丁的未拉伸部分,并且只覆盖了九个补丁的拉伸部分.

这太棒了......但令人困惑,为什么?我可能无法深入挖掘源,但这听起来反直觉(但很好).我想知道原因.所以我在这里发布.

因为我将其标记为android-L,因为我正在开发一个.但我认为这应该是从像姜饼这样的东西.(只是用其他东西替换波纹可绘制,也许是插入抽屉等)

cor*_*992 6

这种效果是由两件事的结合引起的:

  1. 所有九个补丁抽屉都具有从内容区域的边缘自动定义的填充区域.内容区域可以使用边框处的右线和底线明确定义,也可以隐式地从左侧和顶部线定义的可伸展区域定义.

  2. 层列表默认情况下将每个层上的填充累积应用于下一层*,有效地将每个层视为前一层的内容.

*Lollipop引入了一个用于禁用此行为的新属性.