Nine-patch scaling on Android - android

I have a 9-patch that goes like this:
I'm using it as a background for a rectangular area in an Android app. I expect that the center pixel takes the majority of the area, the pixel right above is scaled to a one pixel thick horizontal line underneath the frame, the one to the left forms a one pixel thick vertical line, and so forth. All the 9-patch guides state that.
However, users are reporting color artifacts on display, as if those pixels are scaled in the other direction, too:
What am I missing?

Related

what is different color meaning in 9 patch images

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.

Understanding 9-patch drawables

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.

9Patch image not strectching

I am using http://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create a 9Patch image which is attached. . I use this is to set a drawableBottom to the TextView.But this does not stretch to the width the text view. What am i doing wrong ? This is the original image
Here are a few reasons for it to "not stretch"
Guides must only be one pixel wide, so if you want a 48×48 button, your png will actually be 50×50. Anything thicker than one pixel will remain part of your image.
guides must be solid black (#000000). Even a slight difference in color (#000001) or alpha will cause it to fail
MOST IMPORTANT: you should keep in mind that remaining area of the one-pixel outline must be completely transparent. This includes the four corners of the image – those should always be clear. This can be a bigger problem than you realize. For example, if you scale an image in Photoshop it will add anti-aliased pixels which may include almost-invisible pixels which will also cause it to fail*. If you must scale in Photoshop, use the Nearest Neighbor setting in the Resample Image pulldown menu (at the bottom of the Image Size pop-up menu) to keep sharp edges on your guides.
http://radleymarx.com/blog/simple-guide-to-9-patch/
You need to use android sdk draw9patch tool to make a 9-patch image.
you will need to define stretchable patches to image border like shown in below image border.

9-Patch drawable dimensions Android. How do different densities handle the non-stretched areas?

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.

Android Nine patch Image [duplicate]

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.

Categories

Resources