NinePatchDrawable 来自 Drawable.createFromPath 或 FileInputStream

发布于 2024-10-04 21:30:59 字数 945 浏览 4 评论 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);

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-10-11 21:30:59

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

String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);

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:

String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);
鯉魚旗 2024-10-11 21:30:59

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

I have a very similar problem. I'm trying to create a NinePatchDrawable from a Bitmap that I've retrieved over the network. I still haven't found a solution to that problem. Since the Drawable.createFrom... methods apparently doesn't work, my only option seems to be to understand how to format the nine-patch chunk, and to invoke the NinePatch constructor. See the SO-thread for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文