NinePatchDrawable 来自 Drawable.createFromPath 或 FileInputStream
当从 Drawable.createFromPath 或使用 InputStream 的许多其他方法加载时,我无法使九个补丁可绘制工作(即它正在拉伸九个补丁图像)。
从资源加载时,加载相同的九个补丁效果很好。这是我正在尝试的方法:
Button b = (Button) findViewById(R.id.Button01);
Drawable d = null;
//From Resources (WORKS!)
d = getResources().getDrawable(R.drawable.test);
//From Raw Resources (Doesn't Work)
InputStream is = getResources().openRawResource(R.raw.test);
d = Drawable.createFromStream(is, null);
//From Assets (Doesn't Work)
try {
InputStream is = getResources().getAssets().open("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From FileInputStream (Doesn't Work)
try {
FileInputStream is = openFileInput("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From File path (Doesn't Work)
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");
if (d != null) b.setBackgroundDrawable(d);
I cannot get a nine patch drawable to work (i.e. it is stretching the nine patch image) when loading from either Drawable.createFromPath or from a number of other methods using an InputStream.
Loading the same nine patch works fine when loading from when resources. Here's the methods I am trying:
Button b = (Button) findViewById(R.id.Button01);
Drawable d = null;
//From Resources (WORKS!)
d = getResources().getDrawable(R.drawable.test);
//From Raw Resources (Doesn't Work)
InputStream is = getResources().openRawResource(R.raw.test);
d = Drawable.createFromStream(is, null);
//From Assets (Doesn't Work)
try {
InputStream is = getResources().getAssets().open("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From FileInputStream (Doesn't Work)
try {
FileInputStream is = openFileInput("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From File path (Doesn't Work)
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");
if (d != null) b.setBackgroundDrawable(d);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我回答了一个非常类似的问题这里关于非资源-基于九个补丁。要从 .apk 档案中读取文件,我使用如下方法:
I answered a very similar question here about non-resource-based ninepatches. To read a file from within .apk archives, I use something like this:
我有一个非常相似的问题。我正在尝试从通过网络检索到的
Bitmap
创建一个NinePatchDrawable
。我还没有找到解决这个问题的办法。由于Drawable.createFrom...
方法显然不起作用,我唯一的选择似乎是了解如何格式化九个补丁块,并调用NinePatch
构造函数。有关详细信息,请参阅 SO 线程。I have a very similar problem. I'm trying to create a
NinePatchDrawable
from aBitmap
that I've retrieved over the network. I still haven't found a solution to that problem. Since theDrawable.createFrom...
methods apparently doesn't work, my only option seems to be to understand how to format the nine-patch chunk, and to invoke theNinePatch
constructor. See the SO-thread for details.