开发者

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:

Drawing a 9-patch image on a canvas with drawBitmap

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:

Drawing a 9-patch image on a canvas with drawBitmap


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)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜