Android NinePatchDrawable from Drawable.createFromPath或FileInputStream
当从drawable.createFromPath或使用InputStream的许多其他方法加载时,我无法使九个补丁drawable工作(即,它正在拉伸九个补丁的图像) 从when资源加载时,加载相同的九个补丁可以正常工作。以下是我正在尝试的方法:Android NinePatchDrawable from Drawable.createFromPath或FileInputStream,android,drawable,nine-patch,Android,Drawable,Nine Patch,当从drawable.createFromPath或使用InputStream的许多其他方法加载时,我无法使九个补丁drawable工作(即,它正在拉伸九个补丁的图像) 从when资源加载时,加载相同的九个补丁可以正常工作。以下是我正在尝试的方法: Button b = (Button) findViewById(R.id.Button01); Drawable d = null; //From Resources (WORKS!) d = getResources().getDrawabl
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);
我有一个非常类似的问题。我正试图从通过网络检索的
位图
创建一个NinePatchDrawable
。我还没有找到解决那个问题的办法。由于Drawable.createFrom…
方法显然不起作用,我唯一的选择似乎是了解如何格式化九块补丁,并调用NinePatch
构造函数。有关详细信息,请参阅。我回答了一个非常类似的关于非基于资源的ninepatches的问题。要从.apk归档文件中读取文件,我使用以下方法:
String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);