Android 9补丁-仅允许背景可绘制区域的特定区域伸展 [英] Android nine patch - allow only a specific area of a background drawable to stretch

查看:80
本文介绍了Android 9补丁-仅允许背景可绘制区域的特定区域伸展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有三角形穿孔边界的View?

How does one go about creating a View that has boundaries with triangular perforations ?

到目前为止,我一直在使用背景可绘制对象来实现此目的.当View的尺寸固定时,此方法有效.现在,我遇到了View height 不恒定的情况,因此我需要动态设置height变量.我不能再使用固定高度的背景可绘制对象了.

I've been using background drawables to achieve this so far. That works when the dimensions of the View are fixed. Now I have a situation where the height of the View is not constant, so I need to have the height variable on the fly. I can't use a background drawable with a fixed height anymore.

这是原始背景可绘制对象:

Here is the original background drawable:

这就是最终的View的外观:

解决同一问题的另一种方法是,我们可以允许图像中心伸展而不会扭曲边界吗?如果可以这样做,我们可以使用现有的drawable作为背景.

Another way of looking at the same problem is, can we allow the center of the image to stretch without distorting the boundaries ? If we could do this, we could use the existing drawable as a background.

如何实现?有人遇到过这个问题吗?框架是否具有解决此类问题的现有方法?

How can this be achieved ? Has anyone else faced this problem before ? Does the framework have an existing way of dealing with this class of problems ?

推荐答案

您可以生成九个补丁的png,而不是使用普通图像.

Instead of using a normal image, you can generate a nine-patch png.

为此使用 Simple Nine-patch Generator .

诀窍是左侧的黑线.这就告诉Android,png可以沿该区域垂直扩展.

The trick is the black line on the left side. That tells Android that the png is vertically expandable along this area.

请参见以下示例:

将其保存为九个补丁,格式为imagename.9.png

Save it as a nine-patch, in the format imagename.9.png

这篇关于Android 9补丁-仅允许背景可绘制区域的特定区域伸展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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