建设9补丁绘制在运行时 [英] Building a 9 patch drawable at runtime

查看:134
本文介绍了建设9补丁绘制在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地建立一个9patch绘制在运行在Android 3.0+使用由布赖恩·格里菲(发现的这里)。

I am successfully building a 9patch drawable at runtime on Android 3.0+ using the excellent gist provided by Brian Griffey (found here).

基本上我从网上下载的原始(无补丁)的图形文件,文件名中包含了我需要的,以便相应地缩放图像使用帽插图。然后使用这些值与上述发现的类,并应用图像作为背景的​​各种元素(如的TextView 的ImageButton 按钮的ViewGroup 等)。

Essentially I load the raw (no patches) graphic file from the network and the filename contains the cap insets that I need to use in order to scale the image accordingly. I then use these values with the class found above and apply the image as a background to a variety of elements (such as TextView, ImageButton, Button, ViewGroup, etc).

这完美的作品,你可以在这里看到:

This works perfectly as you can see here:

但是,运行在Android 2.3.x同一code产生的结果是:

However, running the same code on Android 2.3.x yields the result:

我已经通过在Android中使用的源$ C ​​$ C看着解析9patch图像(<一href="https://github.com/android/platform_frameworks_base/blob/master/include/androidfw/ResourceTypes.h#L45-L103"相对=nofollow>这里和<一href="https://android.googlesource.com/platform/packages/apps/Gallery2/+/c2c0b01fbbf243eee1a11c33ba6dd1ce918a9ce5/src/com/android/gallery3d/ui/NinePatchChunk.java"相对=nofollow>这里),但发现越来越使其正常工作,没有办法。我已经尝试过的一切,我可以在它扔无济于事。

I've looked through the source code used in Android to parse a 9patch image (here and here) but have found no method of getting this to work correctly. I've tried just about everything I could throw at it to no avail.

有关备案,9patch包括各轴三列,一固定的一伸展和一个固定。

For the record, the 9patch consists of three columns on each axis, one fixed, one stretchable and one fixed.

在这里我们希望其他人之前已经解决了这个问题。

Here's hoping someone else has solved this problem before.

在此先感谢。

修改我感兴趣的只是在Android 2.3及以上重复这种行为(我本来2.X)。

EDIT I am only interested in duplicating this behavior on Android 2.3 and above (I originally had 2.x).

编辑#2 :此要点介绍正是我想要做的 +图片来源:

EDIT #2 This gist describes exactly what I'm trying to do + Source image:

编辑#3 图像的大小为22px / 58px(宽/高)和插图是14/6/14/6(上/左/下/右)。

EDIT #3 The size of the image is 22px/58px (width/height) and the insets are 14/6/14/6 (top/left/bottom/right).

推荐答案

放在一起都是我到目前为止已经发现,和磨炼了一点:

Putting together all I've spotted so far, and honing it a bit:

  • There doesn't seem to be a difference in the Res_png_9patch structure (which the byte chunk goes into) between android versions (see http://code.metager.de/source/xref/android), so that doesn't look like the cause.

大约九补丁可绘制的其它的答案提示,每个区域(​​尤其是可拉伸的)应当至少2×2像素(如果不是3×3),但外面越小越好。

Other answers about nine patch drawables suggest that each region (especially stretchable ones) should be at least 2x2 pixels (if not 3x3), but outside of that smaller is better.

然而,一些字节组块的分配的方式看起来可能被更新。尝试将第4个字节到9(补丁的数量,我认为),增加7个 NO_COLOR S到结束,移动它的尺寸可达56 +(7× 4)= 84字节

However, the way some of the byte chunk is allocated looks like it could be updated. Try setting the 4th byte to 9 (the number of patches, I think), adding 7 more NO_COLORs to the the end and moving its size up to 56 + (7 x 4) = 84 bytes

这篇关于建设9补丁绘制在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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