Performance: 9-patch vs Custom drawable

What are (if any) are the performance gains for using a 9-patch image with rounded corners for say the background of a dynamically changing String of text, vs a custom Shape drawable that's programmatically generated by Android using Canvas or XML?

A coworker of mine insists on using custom Shape drawables for everything, and all my research points to this being a bad idea for performance reasons.

If you can provide some benchmarks that would be amazing. My coworker is more easily convinced by benchmarks...

Posted Aug 22, 2014, 2:26 PM UTC · edited

88
QuestionsEverythang•

Custom drawable as in a .png image in your resources or a .xml? I believe xml files are better for performance and easier to maintain if you know how to make backgrounds from them and you're not good at Photoshop.

1
finaldeveloperOP•

I meant either an xml shape or using Android Canvas APIs. I should have been clearer. Sorry!

1
QuestionsEverythang•

I think you're talking about a custom View, not a custom drawable.

Usually you would want to make a custom View for added functionality (e.g. making a button that automatically bounces every time it's clicked), but if you're just wanting, say, a button that looks a certain way, 8 times out of 10 a 9-patch or xml is not only easier to program for but probably better for performance.

1
finaldeveloperOP•

To a certain extent, yeah, a custom View.

I would think an xml shape would be more expensive to draw than 9-patch though. Yes?

1
adrock3000•

they are definitely less in file size and scale easier across different dpi's. if you have a 9 patch image(even super small), you have to replicate it for each resolution. a drawable.xml isn't replicated.

i can't confirm off the top of my head but i'm pretty sure it's less of a memory footprint as well to use xml drawable vs png's.

1
nic0lette•

XML drawables use a lot less memory than PNG images, as even if the 9-patch is only 10x10 pixels, if the box you're filling is 100dp x 100dp, the 9-patch is expanded and drawn as a byte array (that you can see in DDMS) The XML drawable doesn't seem to be created as a bitmap, but is actively drawn by the (now) OpenGL framework, probably from primitives?

2
tidderkrow•

Might have to make those benchmarks yourself.

Please share!

1
madisp•

9patch is basically a 3x3 quad texture if you're going with GPU-accelerated rendering so basically it's crazy fast.

1