9-Patch
Posted: Tue Jul 29, 2014 10:48 pm
What is a 9-Patch?
Before answering the question, let me ask you another: How would you realize a gui like that?
It sure aren't scaled bitmaps as we can see sharp, clear edges and smooth gradients. So is it drawn in runtime, using prims or ruby? You know very well that it is not an easy task and will add to the cpu load. So, is it just one large bitmap, containing all elements? But what if you'd want to change something, like the size of one of the elements, or the color?
What if I told you, that it is none of the above? That it is an editable collection of items? That you can control each of them separately? And that you still need to only store a minimum amount of data? What if I told you, that all the elements are created from one simple, small bitmap - the one that you see at the bottom?
What you see there are 9-Patches. The technique behind it is rather simple. A bitmap is sliced into 9 patches of user-defined sizes, and each patch is scaled independantly based on its position in the bitmap. You can have as many 9-Patch-Modules as you like. Feed them all the same source bitmap to get varying sizes or shapes (by changing the patch sizes). Or feed them with different bitmaps. When you save you schematic, you only save the source bitmap(s). And they can be as small as needed to create the effects you want.
The advantages are immense. For example, it is much quicker than drawing in runtime (some refer to it as 'vector drawing', although not the right term). Just a tiny amount of data is stored with the schematic. Still you are as flexible as drawing in runtime.
A simple calculation: The gui above is of size 519x343. If you'd made just one bitmap of the whole gui, you'd store 695 kB. Since the gui is generated from just the small bitmap at the bottom, you now only store 4 kB.
For me it is the most important module for the graphical interface.
Just feed the module with a bitmap, change to the property view and set the margins. Done.
@Nubeat7
If you'd like to make a ruby version, that would be awesome!
Before answering the question, let me ask you another: How would you realize a gui like that?
It sure aren't scaled bitmaps as we can see sharp, clear edges and smooth gradients. So is it drawn in runtime, using prims or ruby? You know very well that it is not an easy task and will add to the cpu load. So, is it just one large bitmap, containing all elements? But what if you'd want to change something, like the size of one of the elements, or the color?
What if I told you, that it is none of the above? That it is an editable collection of items? That you can control each of them separately? And that you still need to only store a minimum amount of data? What if I told you, that all the elements are created from one simple, small bitmap - the one that you see at the bottom?
What you see there are 9-Patches. The technique behind it is rather simple. A bitmap is sliced into 9 patches of user-defined sizes, and each patch is scaled independantly based on its position in the bitmap. You can have as many 9-Patch-Modules as you like. Feed them all the same source bitmap to get varying sizes or shapes (by changing the patch sizes). Or feed them with different bitmaps. When you save you schematic, you only save the source bitmap(s). And they can be as small as needed to create the effects you want.
The advantages are immense. For example, it is much quicker than drawing in runtime (some refer to it as 'vector drawing', although not the right term). Just a tiny amount of data is stored with the schematic. Still you are as flexible as drawing in runtime.
A simple calculation: The gui above is of size 519x343. If you'd made just one bitmap of the whole gui, you'd store 695 kB. Since the gui is generated from just the small bitmap at the bottom, you now only store 4 kB.
For me it is the most important module for the graphical interface.
Just feed the module with a bitmap, change to the property view and set the margins. Done.
@Nubeat7
If you'd like to make a ruby version, that would be awesome!