【发布时间】:2014-11-04 04:17:09
【问题描述】:
我无法深入了解 android 是如何实现其 layer-list drawable 的。但我觉得这很有趣,我几乎不知道为什么会发生这种情况。
以下是一些可绘制对象:
九个补丁 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>
形状 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>
波纹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>
包含以上所有内容的图层列表
<?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>
不幸的是,我仍然无法在我的 L 预览中使用我的屏幕截图,但我可以描述它。
我得到的是形状(我显然没有明确设置它的大小)并没有覆盖整个九块!九块的未拉伸部分神奇地被认为是某种“自动填充的东西”。我所期待的(好吧,我期待的正是 android 为我所做的,我的意思是我的意思......假设......)不是那么积极:不是特别大小的形状可绘制覆盖整个九个补丁就好像后者是一个普通的png一样。
但该形状确实神奇地避开了九个补丁的未拉伸部分,并且仅覆盖在九个补丁的拉伸部分上方。
这太棒了……但令人困惑,为什么?我可能无法深入挖掘源代码,但这听起来确实违反直觉(但很好)。不过我想知道原因。所以我把这个贴在这里。
因为我正在开发一个,所以我将其标记为 android-L。但我认为这应该从姜饼之类的东西上工作。(只是用其他东西替换波纹可绘制,也许是插图可绘制等)
【问题讨论】:
-
您看到的效果是由于九个补丁可绘制对象自动从边缘的不可拉伸区域隐式定义其填充(如果未明确定义),并且图层列表应用每个前一层的填充在后续层上累积。在从多个部分创建可绘制对象时,这种组合非常有用。
-
@corsair992 这真的很有帮助。我知道该功能确实来自某些东西,但从未弄清楚在哪里。很高兴知道 9-patch-drawable 是有效的。你可以把它作为一个答案,因此我应该选择它作为接受的答案。
标签: android android-layout drawable android-5.0-lollipop nine-patch