views:

466

answers:

1

Hello

I'm trying to draw a nine patch onto a Canvas object on the Android. What seems strange is that although I generated my nine patch using the draw9patch tool, the constructor for NinePatch requires an additional byte array called the "chunk" to construct the nine patch.

Why isn't this simpler? What is the "chunk"? And if you have done this yourself, how did you go about it?

Any help appreciated.

+5  A: 

Can you not just do:

Drawable d = Resources.getDrawable(R.drawable.my_nine_patch);
d.draw(canvas);
mbaird
But I need the image to fit a specific size. Otherwise I wouldn't use a nine-patch.
Tom R
In the code I put in above, the object 'd' is actually an instance of NinePatchDrawable since you gave getDrawable the ID of a 9patch resource. So you could do NinePatchDrawable npd = (NinePatchDrawable) d; Will that not work for your purposes?
mbaird
Just tried it: works perfectly. Thank you very much
Tom R
Casting it is not necessary, but you need to call setBounds() on the Drawable before drawing it.
Romain Guy
Brilliant, I could not find this anywhere in the docs. Thank you so much.
Rickard Lindroth