NinePatchDrawable从Drawable.createFromPath或的FileInputStream [英] NinePatchDrawable from Drawable.createFromPath or FileInputStream

查看:546
本文介绍了NinePatchDrawable从Drawable.createFromPath或的FileInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让一个九补丁绘制工作使用InputStream从任Drawable.createFromPath或从一些其他方法加载时(即其拉伸九片图像)。

装载的9个补丁从资源时加载时工作正常。下面是我想要的方式:

 按钮B =(按钮)findViewById(R.id.Button01);可绘制D = NULL;//从资源(作品!)
D = getResources()getDrawable(R.drawable.test);//从原始资源(不工作)
InputStream为= getResources()openRawResource(R.raw.test)。
D = Drawable.createFromStream(是,NULL);//从资产(不工作)
尝试{
InputStream为= getResources()getAssets()开(test.9.png)。;
D = Drawable.createFromStream(是,NULL);
}赶上(例外五){e.printStackTrace();}//从的FileInputStream(不工作)
尝试{
的FileInputStream是= openFileInput(test.9.png);
D = Drawable.createFromStream(是,NULL);
}赶上(例外五){e.printStackTrace();}//从文件路径(不工作)
D = Drawable.createFromPath(getFilesDir()+/test.9.png);
如果(D =空!)b.setBackgroundDrawable(四);


解决方案

我回答了一个非常类似的问题<一href=\"http://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime/5519768#5519768\">here关于非资源型ninepatches。要由.apk档案中读取一个文件,我用的是这样的:

 字符串的文件路径=COM / kanzure /图像/ thx1138.png
InputStream的流=的getClass()getClassLoader()的getResourceAsStream(文件路径)。

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);

解决方案

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);

这篇关于NinePatchDrawable从Drawable.createFromPath或的FileInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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