开发者

Drawing a 9-patch image on a canvas with drawBitmap

开发者 https://www.devze.com 2023-02-26 06:33 出处:网络
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);

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

精彩评论

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

关注公众号