开发者

NinePatchDrawable constructor ignores padding?

开发者 https://www.devze.com 2023-04-13 07:03 出处:网络
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 ni

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 Nine开发者_JAVA百科PatchDrawable 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号