Part 1
If 9-patch images must have a one pixel stretchable area for regions 2, 4, 6, 8 (counting the areas in reading order), how can 9-patch images capture an image such as the one below, where the corners are 4x4, and each of the stretchable areas is 4-pixels wide?
Part 2
Why does sdk/tools/draw9patch make it possible to modify the top and bottom areas independently? Likewise, why are the left and right areas independent? I would be expecting the editor to provide the means to manipulate just the four carets that appear in the image above.
The 4 borders must be 1px tall (or wide) and transparent.
They may or may not have the 9 patch markers 1 px tall (or wide) and tall and black.
The top border is used to mark (zero), one or more stretchable horizontal area/s.
The left border is used to mark (zero), one or more stretchable vertical area/s.
The (optional) right border is used to set the vertical content area (in practice, it marks some internal padding).
The (optional) bottom border is used to set the horizontal content area (in practice, it marks some internal padding).
More is found in this article:
http://blog.booleanbites.com/2012/11/android-how-to-use-9-patch-png.html
For example
Either of the following two images is suitable as a 9-patch, subject to the following constraints:
The quarter-circles at the four corners must maintain their aspect ratio, even if the aspect ratio of the image is modified from its original.
Likewise the circles in the border must remain circles (i.e., they may not be scaled).
Also:
The specification of 9-patch images is under-constrained, namely:
Nothing is specified regarding the four green corners. They can have any value.
Since we specify the zones that will be scaled when needed, rather those that may not be scaled, there is leeway in the specification of the pink single-pixel borders. Either the top or the bottom will do. However, in this example, to maintain the circles in the center of the borders, the width of the pink pixels must be equal.
In this example we look specifically at the case where the intention is to create a variable-sized frame. For this to work as intended the transparency must be set correctly:
The transparency of the inner grey region is full-transparent.
The transparency of the pink pixels is irrelevant (?).
The transparency of the cyan pixels must be full opaque.
Related
I want to understand what is this color change means?
this is only an example..i want to understand what is 9 patch for and how it works
i changed my png to 9.png and after i make some changes (this is NOT that image this only for reference )
i notice that some color is changed in images
i just want to know how to understand which color for what ?
and how to use it ?
The black lines at the edges of the image act as a guide to the nine-patch to determine which parts of the image are scalable, and which parts need to be kept exact. The black lines on the bottom and right edge represent the fill area, basically the total bounds from which scalable areas can be selected. This is typically the length of the flat edge before the corners begin.
The black lines on the top and left edges represent the scalable area. When the image is resized, these lines show where the image can be safely expanded and contracted while still maintaining its appearance. You'll notice that in your example image there is a small gap in the left edge - this is so the speech bubble spike is not expanded along with the rest of the bubble.
And to answer your question on the colours, green zones show areas of the image that can resized in one dimension only (only height or width), while pink areas show parts of the image where both dimensions can be resized. The purple areas in the right-hand preview show the total space expansion.
Radley Marx explains things a little better in this blog post.
Consider the above image.
- The dotted line demarcates the 9-Patch png I will slice out of a photoshop file. I need it to create a popup box.
- The box incorporates a dropShadow as shown by the measuring tool in this photo.
- The pink lines are there to show how I will use the draw9Patch tool to create the 9-Patch.
My question is: If I have a View "Container" with the 9-Patch for a background I need to ensure its children views are always inside the white box. I was going to use padding for this. I was going to set the padding to equal the measuring tool. So if it is 30px in photoshop I'll set layout_paddingLeft"=30dp" for the container. (The design is at MDPI so I assume this conversion is okay). However how do screens of different densities handle the 9path. For instance will the measured area be 30px or 30dip ?
Via the draw9patch tool you can define:
vertical stretching: the black pixels on the left side
horizontal stretching: the black pixels on the top side
vertical content: the black pixels on the right side
horizontal content: the black pixels on the bottom
Note that stretching pixels don't have to be contiguous, so you can exclude some specific slice from stretching (look at the popup arrow above). At he same time you can make a reasonable idea of where your content will be placed just taking a look at the preview on the right, with the violet areas in. As you can suppose, this way you don't have to specify any padding in your layout: the view will take into account those values using the 9patch you set as background.
The no-stretching areas scale with the pixel density. So, if you set the 9patch above as an mdpi asset, the top-leftmost slice is rendered in a 50x40 pixels area #mdpi, and in 100x80 pixels area #xhdpi. The leftmost stretching areas instead arrange their width according to dpi, while height is arranged according to content. Other stretching slices work in similar way.
In both cases, dealing with a "low resolution" 9patch can lead to ugly pixelation artifacts. A possible solution is to provide a different 9patch for each supported dpi, or to define only the higher ones (xhdpi) and let Android scale them down accordingly.
The content bounds are handled as dp too, so they scale according to pixel density. For example: a left padding defined in the original 9patch as 40px#mdpi, will be translated in 80px#xhdpi, so the content will never flow out the given bounds. Note you can even override the content bounds specified in 9patch via the padding* properties in your layout.
First i had a button as this:
<Button
android:id="#+id/prefButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Edit" />
since i didn't specify and textSize, it takes the default side from the device font settings. Add even the Button width and Height looks good.
Now the problem is when i want to add a background image(actually a background color but couldn't get the exact argb value, hence using image), i added:
android:background="#drawable/backgroundImage"
and the image was some 300x300 px sized. Hence now i get the Button of size 300x300 (obviously).
So, now i have to hardcode the value of Height and Width. and check if it looks good with those values, if it doesn't, i must also hardcode TextSize again.
Is there a way to keep the Button same as before and just add a background image(might be bigger).
Thank You
The better way I can think of approaching your problem is to make your image smaller and setting it as a 9patch image, that would make it stretch to fill the text just as the default button image would do. In a 9patch image you can define the stretching parts of your image so it wouldn't be deformed when it stretches. Here you can see how to draw a ninepatch stretchable parts.
Here is an explanation of what a ninepatch image is, taken from here:
Nine-patch
A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.
The border is used to define the stretchable and static areas of the image. You indicate a stretchable section by drawing one (or more) 1-pixel-wide black line(s) in the left and top part of the border (the other border pixels should be fully transparent or white). You can have as many stretchable sections as you want: their relative size stays the same, so the largest sections always remain the largest.
You can also define an optional drawable section of the image (effectively, the padding lines) by drawing a line on the right and bottom lines. If a View object sets the NinePatch as its background and then specifies the View's text, it will stretch itself so that all the text fits inside only the area designated by the right and bottom lines (if included). If the padding lines are not included, Android uses the left and top lines to define this drawable area.
To clarify the difference between the different lines, the left and top lines define which pixels of the image are allowed to be replicated in order to stretch the image. The bottom and right lines define the relative area within the image that the contents of the View are allowed to lie within.
Here is a sample NinePatch file used to define a button:
This NinePatch defines one stretchable area with the left and top lines and the drawable area with the bottom and right lines. In the top image, the dotted grey lines identify the regions of the image that will be replicated in order to stretch the image. The pink rectangle in the bottom image identifies the region in which the contents of the View are allowed. If the contents don't fit in this region, then the image will be stretched so that they do.
The Draw 9-patch tool offers an extremely handy way to create your NinePatch images, using a WYSIWYG graphics editor. It even raises warnings if the region you've defined for the stretchable area is at risk of producing drawing artifacts as a result of the pixel replication.
I would like to know why do we use nine-patch ? I know is to shrink or stretch images but if I want to resize an image can't we just do it on a dedicated image editor like gimp for example ?
What is 9-Patch?
9-Patch images are stretchable, repeatable images reduced to their smallest size; users draw a right and bottom, solid black 1 pixel border to tell the system how to place the content within the image.
The 9-Patch is a PNG image with an added extension to the file name (filename.9.png), which allows the Android system to determine how the image can be stretched and contorted to meet the specific layout constraints.
The Android operating system reads the borders of these images to understand how to properly stretch the image itself and the content within the image such as text and effects.
9-Patch Theory
9-Patch gets its name from the fact that the overlay created breaks the image up into nine defined regions. Each region has specific stretch properties:
Corner Regions (1, 3, 7, 9)
These regions are fixed and nothing inside them will stretch.
Horizontal Sides (4, 6)
The pixels in these region will stretch vertically when necessary.
Vertical Sides (2, 8)
The pixels in these region will stretch horizontally when necessary.
Center (5)
The pixels in this region will stretch in both horizontal and vertical directions equally.
here is Google docs
Nine patch image is very useful because it reduces your resource and one can maintain the curve shape which get stretch in normal .png.
Reduces resource : One can make a small NinePatch image and can stretch it as more as he can by repeating Pixel
Maintained border corner even if display size changes.
No need to give padding programmatically, you can reserve text area in NinePatch directly.
The top and left pixel border define the stretchable area. The bottom and right, however, define the CONTENT area. If you want the padding to go away, you need to make the bottom and right bar extend all the way to the edge of the artwork (not all the way to the corner pixels, though!). Basically, the right and bottom pixel border define your padding.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://www.developer.com/ws/android/programming/Working-with-NinePatch-Stretchable-Graphics-in-Android-3889086.htm
The advantage of using 9-patch images is that when using it as a background, for instance, the image won't stretch and loose proportions in different screen sizes. the center 'patch' will remain as is and the 'borders' patches will be stretched to fit the screen/view size.
one more and biggest advantage is memory. Same small size memory can be reused for different screen size devices.Well-designed 9-patch images are less error-prone and have high reusability. I had hard time optimizing the UI for different resolutions until I knew that Android supports 9-patch.
For padding as #hotveryspicy said you can use the padding box ( where your text button will be filled) to define your paddig values and they are defined like this:
padding-top: distance between the top edge of the padding box and the
top edge of your button
padding-bottom: distance between the buttom edge of the padding box and
the buttom edge of your button
padding-right: distance between the right edge of the padding box and
the right edge of your button
padding-left: distance between the left edge of the padding box and the
left edge of your button
Hope this will help you to have a clear idea and how important 9-patch drawable are
Nine-patch is used for dynamic stretching and shrinking of an image at runtime. That's the reason why it cannot be compared to statically resizing an image using an image editor.
Nine-patch is used for things like borders that dynamically size according to the content, so they have to stretch dynamically.
9-patch images aren't just scaled up; they're "stretched" in a defined way. The classic case is a button with rounded corners. If the button was just scaled, the radius of the corners would be enlarged too. With 9-patch images, the corners can be defined to stay the same size while the lengths of the edges are increased.
have you worked with css. if not then there is one property called repeat which gives you ability to repeat 1px image in to the width of 1040 and even more with out starching
9 path do the same, some time due to the different resolution of the images rather creating separate image for each phone create 9 patch image
Hope that help
Nine-patch allow you to strech just a part of an image, and not the whole image. It can be useful to design for example custom buttons, EditTexts, etc...
You can lean more here: http://developer.android.com/tools/help/draw9patch.html
Nine-patch is to do the stretching on the run time... If you use an Button with a custom background for example and say width-> fill_parent... there is a lot of different devices out there with different resolution how are you going to prepare images for all of them... you give a nine patch and its stretch on the run.
I am trying to use the attached nine patch image as the background for a View in my app (the image is from the android-15 sdk resources). As pictured, the image has two vertical dots to the left of image, two horizontal dots above the image, and a horizontal line below the image. These dots/line from the images show in my app.
I copied the file from the sdk folder into my resources directory, and set the drawable resource as the background for my view. What is the proper way to use this image as the background of a view, such that the dots/line do not show?
By stretchable patches, you're basically telling Android which rows and columns of pixels in the image that you want to repeat. When you stretch a standard image, there are two possibilities: One, it is scaled proportionally, but still loses sharpness due to interpolation; Two, it is scaled disproportionately, and loses not only its sharpness, but its shape as well. An example of a disproportionate scaling is below:
So the purpose of the black lines it tell Android what areas of the image are safe to repeat. The top corner defines the column(s) that it can stretch, while the left corner defines the row(s) that can stretch. The bottom and right corners just define the actual content area (e.g. where the button is allowed to place text), you can reserve extra space to pad the frame. In the image below, you can see that the two black pixels on the outside of the frame define rows on the image, while the one on top defines a column.
And below, this shows the result of a 9-patched stretched out to various sizes. If it's enlarged to be wider, the halves of the image on the left and right of the defined stretchable column are aligned to the left and right of the new size, and the defined column is repeated to fill the space in between. Same thing happens with the defined rows; if you use multiples (I don't believe you can use more than 2 stretch rows/columns) it just evenly pads the space with both of them; in this case I used it to keep the gradient evenly split down the middle.
The lines and dots are what make this a NinePatch. The dots on the left side and the top define the area that can stretch, while the other two lines define the bounding box for drawing things inside the NinePatch.
If you set such an image as a background - those lines will not be shown. To create these images yourself, you can use the draw9patch tool in the android sdk (http://developer.android.com/guide/developing/tools/draw9patch.html).
One thing to remember, is that the file type for a NinePatch image has to be, for example: image.9.png. Otherwise those lines will be drawn.
Do you use the draw9patch app located in your tools folder? This app can be used to simply create a nine patch image.