Drawing a 9-patch image on a canvas with drawBitmap
What is the proper way to use a 9-patch image resource on a custom view?
I'm doing this:
background = BitmapFactory.decodeResource(getResources(), R.drawable.fundo_bookshelf);
canvas.drawBitmap(background, null, new Rect(0, y, width, y+backgroundHeight), null);
fundo_bookshelf is a proper 9-patch image, saved as fundo_bookshelf.9.png, seen below:
But when using the drawBitma开发者_开发问答p, the whole image is stretched, instead of only the part with the black pixel on the top, as shown on this screenshot:
Something like this?
NinePatchDrawable bg = (NinePatchDrawable) resources.getDrawable(id);
if (bg != null) {
bg.setBounds(0, 0, getWidth(), getHeight());
bg.draw(canvas);
}
(sorry I didn't adapt it to your code, that is a straight cut/paste from my own, but hopefully enough to go on)
精彩评论