如何从多个图像制作可绘制对象?
我有三个 9-patch PNG,它们一起构成了按钮的背景(左侧、中间、右侧)。我想将这三个图像组合在一起,形成一个可绘制的图像,我可以将其指定为 XML 中按钮的背景,类似于:
res/drawable/button_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/button_left_side" />
<nine-patch android:src="@drawable/button_middle" />
<nine-patch android:src="@drawable/button_right_side" />
res /layout/main.xml:
<button android:background="@drawable/button_background" />
这可能吗?
I have three 9-patch PNG's which together make up the background for a button (left side, middle, right side). I would like to combine these three images together in a drawable which I can specify as the background for a button in XML, something along the lines of:
res/drawable/button_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/button_left_side" />
<nine-patch android:src="@drawable/button_middle" />
<nine-patch android:src="@drawable/button_right_side" />
res/layout/main.xml:
<button android:background="@drawable/button_background" />
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番尝试,我终于以满意的方式解决了这个问题。我简单地实现了一个图层列表可绘制对象,如下所示:
其中
26px
值是两个按钮侧图像的宽度(以像素为单位)。要使用此可绘制对象,只需像任何其他可绘制对象一样调用它即可:
这完全按照您的预期工作,中间正常扩展以适合所需的宽度,并且所有三个图像都扩展以适合所需的高度。我希望其他人也能从中受益!
After some trial-and-error, I was able to solve the problem in a satisfactory way. I simply implemented a Layer-List drawable as follows:
Where the
26px
values are the width of the two button side images in pixels.To use this drawable, simply call it like any other drawable:
This works exactly how you would expect it to, with the middle expanding as normal to fit the desired width, and all three images expanding to fit the desired height. I hope others can benefit from this!