在运行时构建 9 块可绘制 [英] Building a 9 patch drawable at runtime

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

问题描述

我使用 Brian Griffey 提供的优秀要点在运行时在 Android 3.0+ 上成功构建了一个 9patch drawable(发现 和 ),所以这看起来不像是原因.

  • 关于 9 个可绘制块的其他答案表明,每个区域(尤其是可拉伸区域)应至少为 2x2 像素(如果不是 3x3),但除此之外越小越好.

  • 然而,一些字节块的分配方式看起来可以更新.尝试将第 4 个字节设置为 9(我认为是补丁的数量),在末尾再添加 7 个 NO_COLOR 并将其大小移动到 56 + (7 x 4) = 84 个字节

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

    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:

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

    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.

    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.

    Thanks in advance.

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

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

    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.

    • 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.

    • 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天全站免登陆