创建NinePatch / NinePatchDrawable在运行时 [英] Create a NinePatch/NinePatchDrawable in runtime

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

问题描述

我有我的Andr​​oid应用程序要求的部件上的图形应该是定制的,通过检索服务器端新的颜色和图像。一些这些图像是九补丁图像。

I have a requirement on my Android application that parts on the graphics should be customizable, by retrieving new colors and images from the server side. Some of these images are nine-patch images.

我无法找到一个方法来创建和显示这九个补丁图像(已通过网络检索)。

I can't find a way to create and display these nine-patch images (that have been retrieved over the network).

九补丁图像检索和保存应用程序中的位图。为了创建一个<一个href="http://developer.android.com/reference/android/graphics/drawable/NinePatchDrawable.html">NinePatchDrawable,您可能需要相应的 NinePatch 或NinePatch的块(字节[])。的NinePatch不能从资源加载,由于图象中不/ RES /抽拉/存在。此外,为了创造NinePatch,你需要的NinePatch的块。所以,这一切都向下钻取到块......接下来的问题是,怎么做一种格式/从现有的位图的块(包含NinePatch信息)?

The nine-patch images are retrieved and kept in the application as Bitmaps. In order to create a NinePatchDrawable, you either need the corresponding NinePatch or the chunk (byte[]) of the NinePatch. The NinePatch can NOT be loaded from the Resources, since the images doesn't exist in /res/drawable/. Furthermore, in order to create the NinePatch, you need the chunk of the NinePatch. So, it all drills down to the chunk ... The question is then, how do one format/generate the chunk from an existing Bitmap (containing the NinePatch information)?

我已经通过Android源$ C ​​$ c和网络搜索,我似乎无法找到这种任何的例子。更糟糕的是,原生地做了NinePatch的所有解码资源似乎... =(

I've searched through the Android source code and the Web and I can't seem to find any examples of this. To make things worse, all decoding of a NinePatch resources seem to be done natively ... =(

有没有人有这样那样的问题任何经验!?任何帮助是极大AP preciated!

Have anyone had any experiences with this kind of problem!? Any help is greatly appreciated!!!

我针对API级别4,如果这是很重要的。

I'm targeting API level 4, if that is of importance.

在此先感谢!

问候, 雅各布

推荐答案

getNin​​ePatchChunk 工作得很好。它返回null,因为你给位图源ninepatch。它需要一个编译ninepatch形象。

getNinePatchChunk works just fine. It returned null because you were giving Bitmap a "source" ninepatch. It needs a "compiled" ninepatch image.

有两种类型的ninepatch文件格式在Android的世界(源和汇编)。源版本中,您添加1px的透明边框everywhere--当你以后编译你的应用程序到一个.apk文件,AAPT将您的* .9.png文件转换到Android的期望二进制格式。这就是PNG文件中获取块的元数据。 (更多

There are two types of ninepatch file formats in the Android world ("source" and "compiled"). The source version is where you add the 1px transparency border everywhere-- when you compile your app into a .apk later, aapt will convert your *.9.png files to the binary format that Android expects. This is where the png file gets its "chunk" metadata. (read more)

好了,现在正事(你听DJ kanzure)。

Okay, now down to business (you're listening to DJ kanzure).

  1. 客户端code,是这样的:

  1. Client code, something like this:

InputStream stream = .. //whatever
Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] chunk = bitmap.getNinePatchChunk();
boolean result = NinePatch.isNinePatchChunk(chunk);
NinePatchDrawable patchy = new NinePatchDrawable(bitmap, chunk, new Rect(), null);

  • 服务器端,你需要prepare图像。您可以使用的Andr​​oid二进制资源编译器。这种自动化有些痛苦远离创建一个新的Andr​​oid项目只是为了一些* .9.png文件编译成Android原生格式。如果你要手动做到这一点,你就基本上是做一个项目,并扔在一些* .9.png文件(源文件),编译一切都成.apk文件格式,解压缩的.apk文件,然后找到*。 9.png文件,这就是你发送给您的客户之一。

  • Server-side, you need to prepare your images. You can use the Android Binary Resource Compiler. This automates some of the pain away from creating a new Android project just to compile some *.9.png files into the Android native format. If you were to do this manually, you would essentially make a project and throw in some *.9.png files ("source" files), compile everything into the .apk format, unzip the .apk file, then find the *.9.png file, and that's the one you send to your clients.

    另外:我不知道,如果 BitmapFactory.de codeStream 知道关于NPTC块在这些PNG文件,因此它可能会或可能不会进行治疗图像流正常。存在 Bitmap.getNin​​ePatchChunk 的建议 BitmapFactory might--你可以去看看它在上游codeBase的。

    Also: I don't know if BitmapFactory.decodeStream knows about the npTc chunk in these png files, so it may or may not be treating the image stream correctly. The existence of Bitmap.getNinePatchChunk suggests that BitmapFactory might-- you could go look it up in the upstream codebase.

    在的情况下,它不知道的NPTC块,你的图像被搞砸了显著,那么我的回答变化不大。

    In the event that it does not know about the npTc chunk and your images are being screwed up significantly, then my answer changes a little.

    而不是发送编译ninepatch图像到客户端,你写一个快速的Andr​​oid应用程序加载编译后的图像和吐出字节[] 块。然后,随着常规image--没有透明边框,而不是源ninepatch形象,而不是编ninepatch图像传送该字节数组给您的客户。您可以直接使用该块,以创建您的对象。

    Instead of sending the compiled ninepatch images to the client, you write a quick Android app to load compiled images and spit out the byte[] chunk. Then, you transmit this byte array to your clients along with a regular image-- no transparent borders, not the "source" ninepatch image, not the "compiled" ninepatch image. You can directly use the chunk to create your object.

    另一种方法是使用对象序列化发送ninepatch图像( NinePatch )给你的客户,如与JSON或内置串行器。

    Another alternative is to use object serialization to send ninepatch images (NinePatch) to your clients, such as with JSON or the built-in serializer.

    修改如果你真的,真的需要构建自己的数据块的字节数组,我会通过查看 do_9patch ,<$ C $启动C> isNinePatchChunk , Res_png_9patch 和 Res_png_9patch ::连载()。还有从梅德斯基巴自制NPTC块的读者。我不能发布的链接,因此,如果有人可以修改我的答案,这将是很酷的。

    Edit If you really, really need to construct your own chunk byte array, I would start by looking at do_9patch, isNinePatchChunk, Res_png_9patch and Res_png_9patch::serialize() in ResourceTypes.cpp. There's also a home-made npTc chunk reader from Dmitry Skiba. I can't post links, so if someone can edit my answer that would be cool.

    do_9patch:<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=tools/aapt/Images.cpp;h=3c471ca76107cc49b5f8e3ca15e87725ed80faf9;hb=refs/heads/gingerbread">http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=tools/aapt/Images.cpp;h=3c471ca76107cc49b5f8e3ca15e87725ed80faf9;hb=refs/heads/gingerbread

    do_9patch: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=tools/aapt/Images.cpp;h=3c471ca76107cc49b5f8e3ca15e87725ed80faf9;hb=refs/heads/gingerbread

    isNinePatchChunk:<一href="http://netmite.com/android/mydroid/1.6/frameworks/base/core/jni/android/graphics/NinePatch.cpp">http://netmite.com/android/mydroid/1.6/frameworks/base/core/jni/android/graphics/NinePatch.cpp

    isNinePatchChunk: http://netmite.com/android/mydroid/1.6/frameworks/base/core/jni/android/graphics/NinePatch.cpp

    结构Res_png_9patch:<一href="https://scm.sipfoundry.org/rep/sipX/main/sipXmediaLib/contrib/android/android_2_0_headers/frameworks/base/include/utils/ResourceTypes.h">https://scm.sipfoundry.org/rep/sipX/main/sipXmediaLib/contrib/android/android_2_0_headers/frameworks/base/include/utils/ResourceTypes.h

    struct Res_png_9patch: https://scm.sipfoundry.org/rep/sipX/main/sipXmediaLib/contrib/android/android_2_0_headers/frameworks/base/include/utils/ResourceTypes.h

    梅德斯基巴的东西:<一href="http://$c$c.google.com/p/android4me/source/browse/src/android/graphics/Bitmap.java">http://$c$c.google.com/p/android4me/source/browse/src/android/graphics/Bitmap.java

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

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