0

当从 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);
4

2 回答 2

1

我在这里回答了一个关于非基于资源的九补丁的非常相似的问题。要从 .apk 档案中读取文件,我使用如下内容:

String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);
于 2011-04-02T03:38:49.203 回答
0

我有一个非常相似的问题。我正在尝试NinePatchDrawableBitmap通过网络检索到的 a 创建一个。我还没有找到解决这个问题的方法。由于这些Drawable.createFrom...方法显然不起作用,我唯一的选择似乎是了解如何格式化九个补丁块,并调用NinePatch构造函数。有关详细信息,请参阅SO-thread

于 2011-02-23T12:59:36.567 回答