问题
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);
回答1:
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);
回答2:
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.
来源:https://stackoverflow.com/questions/4301343/ninepatchdrawable-from-drawable-createfrompath-or-fileinputstream