Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NinePatchDrawable does not get padding from chunk

I need help with NinePatchDrawable:

My app can download themes from the network. Almost all things work fine, except 9-Patch PNGs.

final Bitmap bubble = getFromTheme("bubble");
if (bubble == null) return null;

final byte[] chunk = bubble.getNinePatchChunk();
if (!NinePatch.isNinePatchChunk(chunk)) return null;

NinePatchDrawable d = new NinePatchDrawable(getResources(), bubble, chunk, new Rect(), null);
v.setBackgroundDrawable(d);

d = null;
System.gc();

getFromTheme() loads the Bitmap from the SD card. The 9-Patch PNGs are already compiled, that means they include the required chunk.

The way how I convert the Bitmap to a NinePatchDrawable object seems to be working, because the image is stretchable as well as I drew it.

The only thing that doesn't work is the padding. I already tried to set the padding to the view like this:

final Rect rect = new Rect();   // or just use the new Rect() set
d.getPadding(rect);             // in the constructor
v.setPadding(rect.left, rect.top, rect.right, rect.bottom);

d.getPadding(rect) should fill the variable rect with the padding got from the chunk, shouldn't it? But it doesn't.

Result: The TextView (v) does not show the text in the content area of the 9-Patch image. The paddings are set to 0 in each coordinate.

Thanks for reading.

like image 357
DragonWork Avatar asked Jun 16 '12 18:06

DragonWork


2 Answers

I've never seen an example where the Padding isn't included as part of the 9-patch like so:

enter image description here

To do this you should first construct a NinePatch and then create you're Drawable from it:

NinePatch ninePatch = new NinePatch(bitmap, chunk, srcName);
NinePatchDrawable d = new NinePatchDrawable(res, ninePatch);

However, you seem to be constructing your Drawable with an empty rectangle:

NinePatchDrawable d = new NinePatchDrawable(getResources(), bubble, chunk, new Rect(), null);

If you want to programatically specify the padding try this:

Rect paddingRectangle = new Rect(left, top, right, bottom);
NinePatchDrawable d = new NinePatchDrawable(getResources(), bubble, chunk, paddingRectangle, null);
like image 69
Graeme Avatar answered Sep 22 '22 01:09

Graeme


A bit late to the party, but here is how I solved it:

I use the decoder method that NinePatchDrawable provides, it reads the padding correctly:

     var myDrawable = NinePatchDrawable.createFromStream(sr, null);
like image 29
thumbmunkeys Avatar answered Sep 26 '22 01:09

thumbmunkeys