Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a 9-patch image on a canvas with drawBitmap

Tags:

android

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:

fundo_bookshelf

But when using the drawBitmap, the whole image is stretched, instead of only the part with the black pixel on the top, as shown on this screenshot: strechted image

like image 964
Paulo Cesar Avatar asked Apr 18 '11 20:04

Paulo Cesar


1 Answers

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)

like image 158
rob Avatar answered Nov 14 '22 12:11

rob