Android的:如何建立从InputStream 9patch形象? [英] Android: how to create a 9patch image from an inputstream?

查看:249
本文介绍了Android的:如何建立从InputStream 9patch形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是低于code实例化一个9patch图像,并将其设置为一个按钮的背景。下图显示了非理想的结果。

 的InputStream = MyClass.class.getResourceAsStream(/图片/ btn_default_normal.9.png);
可绘制D = NinePatchDrawable.createFromStream(中,NULL);
button.setBackgroundDrawable(四);

试过低于code为好,这似乎导致由于原生的Andr​​oid code的ANR。这不是真正清楚发生了什么,但应用程序的存在没有任何警告,日志说,一些有关的ANR,我看到下面的堆栈跟踪日志中颇有几分。

 的InputStream = MyClass.class.getResourceAsStream(/图片/ btn_default_normal.9.png);
位图位图= BitmapFactory.de codeStream(中);
字节[] =块bitmap.getNin​​ePatchChunk();
NinePatchDrawable绘制=新NinePatchDrawable(位图,块,新矩形(),NULL);
button.setBackgroundDrawable(绘制); 在android.graphics.NinePatch.validateNinePathChunk(本机方法)
在android.graphics.NinePatch<&初始化GT;
在android.graphics.drawable.NinePatchDrawable<&初始化GT;


解决方案

createFromStream()的方法是绘制对象类(NinePatchDrawable扩展)的一部分,所以它返回给你一个普通可绘制对象,而不是九补丁。这就是为什么你的按钮原来看这样,我怀疑。

在哪里是您试图在一个九宫格的形象呢?它从你的例子看来,这可能与您的应用程序资源包括在(也许在绘制一个文件夹?)如果是这样的话有一个原因,用绘制的ID将不会为你的情况下工作?你应该能够做这样的事:

  button.setBackgroundResource(R.drawable.btn_default_normal);

如果我失去了一些东西(我绝对可以。),而且你不能使用的资源ID的原因。从它看起来像你的code的第二块的文档更接近你想要的东西。

但是,你正在使用的构造函数是德precated。 <一href=\"http://developer.android.com/reference/android/graphics/drawable/NinePatchDrawable.html#NinePatchDrawable%28android.content.res.Resources,%20android.graphics.Bitmap,%20byte%5B%5D,%20android.graphics.Rect,%20java.lang.String%29\"相对=nofollow>尝试使用这一个,而不是这将是这样的:

 中的InputStream = MyClass.class.getResourceAsStream(/图片/ btn_default_normal.9.png);
位图位图= BitmapFactory.de codeStream(中);
字节[] =块bitmap.getNin​​ePatchChunk();
NinePatchDrawable绘制=新NinePatchDrawable(getResources(),位图,块,新矩形(),NULL);
button.setBackgroundDrawable(绘制);

唯一的区别是第一个放慢参数应该是您的应用程序资源的对象。

不过,老实说,我敢发誓我在某些时候,该系统无法与NinePatchDrawables动态为对象的工作读的地方。即使NinePatchDrawable对象存在,我是IM pression,这是不工作/不打算成为公共API的一部分

编辑:

请问在这个问题上的帮助的答案吗? <一href=\"http://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime\">Create一个NinePatch / NinePatchDrawable在运行时

也是我在这个问题的答案底部提醒我究竟为什么我是IM pression下,这是不工作/不indeded的公共API的一部分的为getNin​​ePatchChunk文档


  

返回私人数据的可选阵列,对于一些位图所使用的UI系统。不打算由应用程序调用。


但看起来他们设法得到它反正工作。

I am using the below code to instantiate a 9patch image and set it as the background of a button. The following image shows the non-ideal result.

InputStream = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
Drawable d = NinePatchDrawable.createFromStream(in, null);
button.setBackgroundDrawable(d);

Tried the below code as well, which seems to result in an ANR caused by native android code. It's not really clear what happens, but the application exists without warning, the log says something about an ANR, and I see the following stacktrace quite a bit in the log.

InputStream = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
Bitmap bitmap = BitmapFactory.decodeStream(in);
byte[] chunk = bitmap.getNinePatchChunk();
NinePatchDrawable drawable = new NinePatchDrawable(bitmap, chunk, new Rect(), null);
button.setBackgroundDrawable(drawable);



 at android.graphics.NinePatch.validateNinePathChunk(Native Method)
at android.graphics.NinePatch.<init>
at android.graphics.drawable.NinePatchDrawable.<init>

解决方案

the createFromStream() method is part of the Drawable class (which NinePatchDrawable extends) So it is returning to you a plain Drawable object, rather than a nine patch. That is why your button turns out looking that way I suspect.

Where is the image that you are trying to make in to a nine-patch? It seems from your example that it may be included in with your application resources (perhaps in one of the drawable folders?) if this is the case is there a reason that using the ID of the drawable will not work for your situation? you should be able to do something like this:

button.setBackgroundResource(R.drawable.btn_default_normal);

If I am missing something(which I definitely could be.) and there is a reason you can't use the resource ID. From the docs it looks like your second block of code is closer to what you'll want.

But the constructor you're using is deprecated. Try using this one instead. which would look like this:

InputStream in = MyClass.class.getResourceAsStream("/images/btn_default_normal.9.png");
Bitmap bitmap = BitmapFactory.decodeStream(in);
byte[] chunk = bitmap.getNinePatchChunk();
NinePatchDrawable drawable = new NinePatchDrawable(getResources(), bitmap, chunk, new Rect(), null);
button.setBackgroundDrawable(drawable);

The only difference being the first paramter should be your Applications Resources object.

However, honestly I could've sworn I read somewhere at some point that the system was incapable of working with NinePatchDrawables dynamically as objects. Even though the NinePatchDrawable object exists, I was under the impression that it was not working / not intended to be part of the public APIs

EDIT:

Does the answer on this question help? Create a NinePatch/NinePatchDrawable in runtime

Also my answer at the bottom of that question reminds me exactly why I was under the impression that it was not working / not indeded for part of the public APIs the docs for the getNinePatchChunk say

Returns an optional array of private data, used by the UI system for some bitmaps. Not intended to be called by applications.

But it looks like they managed to get it working anyway.

这篇关于Android的:如何建立从InputStream 9patch形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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