3

I am trying to construct a NinePatchDrawable at run-time from a compiled nine-patch PNG file located in the assets the folder. The problem is that the constructor seems to ignore the padding in the nine-patch chunk.

InputStream inStream = context.getAssets().open(imagePath);
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
byte[] chunk = bitmap.getNinePatchChunk();
boolean isNinePatch = NinePatch.isNinePatchChunk(chunk);
Rect padding = new Rect();
if(isNinePatch)
    drawable = new NinePatchDrawable(context.getResources(), bitmap, chunk, padding, imagePath);

This code returns a NinePatchDrawable with zero padding on all sides. I am positive the PNG file contains the correct padding margins because the following code returns a NinePatchDrawable with the right padding, using the exact same compiled nine-patch PNG file:

InputStream inStream = context.getAssets().open(imagePath);
Drawable drawable = Drawable.createFromResourceStream(context, null, inStream, null);

(Why don't I just use this method instead? Because it crashes on some handsets.)

If anyone can show me how to construct a NinePatchDrawable at run-time with the correct padding I'd really appreciate it!

Community
  • 1
  • 1
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
  • You can refer to the solution here, it work http://stackoverflow.com/questions/11065996/ninepatchdrawable-does-not-get-padding-from-chunk – tsengvn Jan 28 '13 at 07:46

0 Answers0