9Patch image not strectching - android

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.

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.

How do I create a layout w/ a white background, grey border with a triangle arrow - Android

How would I replicate this in Android, my general instinct would be to create a regular layout w/ a border, then have 2 layouts that contain a triangle (one the same color as the border + one that's slightly smaller but white and overlap the two), idk if there's an easier way to do that:
You can use 9patch to achieve something like you wanted.
The Draw 9-patch tool is a WYSIWYG editor that allows you to create
bitmap images that automatically resize to accommodate the contents of
the view and the size of the screen. Selected parts of the image are
scaled horizontally or vertically based indicators drawn within the
image.
A complete guideline can be found here : http://developer.android.com/tools/help/draw9patch.html
The easiest way to reach this, is to set the layout background to that image.
background="#drawable/template"
Probably you want to have the area out of the "bubble" transparent, so you will need to save it as PNG.

Creating & Using 9-patch images in Android

I recently heard about 9-patch images. I know its 9 tiled and is stretchable. I'd like to know more about it.
How can I create a 9-patch image?
Is there any tool? Can I create it from AndroidSDK or code?
Main Advantages of 9-patch over regular png?
(is it stretchable dynamically/ automatically according to screen?)
The SDK and Android Studio both ship with the "Draw 9-patch" tool ("draw9patch" in the SDK tools folder) which is a simple editor. Here is a nicer one which is also open source. It has a simple but clever default image.
The official documentation has improved over the years. In summary, nine patch images' most important advantage is that they can specify (non-contiguous) areas to scale:
A NinePatch graphic is a standard PNG image that includes an extra
1-pixel border. It must be saved with the 9.png extension in the
res/drawable/ directory of your project.
Use the border 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. The relative size of
the stretchable sections stays the same, so the largest section always
remains 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 a
line on the bottom. If a View object sets the NinePatch graphic as its
background and then specifies the view's text, it stretches itself so
that all the text occupies only the area designated by the right and
bottom lines (if included). If the padding lines aren't included,
Android uses the left and top lines to define this drawable area.
But the docs lack good examples. This tutorial has some great examples at the end that answer the second part of your question, explaining how the scaling works - not just for buttons - but also frames, and it has a complete example project that you can download and play with.
Most of the examples talk about creating a 9-patch image, but implementation details are usually left at a high level.
Nick's post above - with the good 9-patch tutorial that provides a working project download file , saved the day.
Here are the main implementation details that worked for me (once you have a 9-patch image ready to go):
Reference the drawable with the name but don't include .9.png (auto-complete in eclipse will take care of this)
Make sure you only have 1 image under the main /drawable folder (not a version for each dpi folder)
The image must be specified using :background, not :src (this got me stuck for a while)
android:background="#drawable/splash_logo"
Make sure the image and layout that contains it are using:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
How can I create a 9-patch image? Is there any tool? Can I create it from AndroidSDK or code?
When you search a major search engine for android 9-patch tool, the very first hit is for the Android developer documentation page on the draw9patch tool.
Main Advantages of 9-patch over regular png? (is it stretchable dynamically/ automatically according to screen?)
The Android developer documentation contains other pages that describe nine-patch PNG files. This documentation includes passages like:
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.
With 9-patch image you can choose which part of your image may be stretched.
It must be png image and name must end with .9.png (something.9.png)
http://developer.android.com/tools/help/draw9patch.html
Simple tool for all densities:
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html
1.What are NinePatch images?
NinePatch images are PNG images that mark the parts of an image that can be stretched. They have an extension like image_name.9.png.
2.Where they are stored in android project ?
res/drawable/image_name.9.png
3.How to create NinePatch image for your anndroid app ?
Android SDK includes a WYSIWIG draw9patch.jar tool inside your Android SDK /tools folder.
Main Advantages of 9-patch over regular png
Actually 9 Patch images are stretchable, repeatable images reduced to their smallest size.the image won't stretch and loose proportions in different screen sizes. 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 re-usability .
https://developer.android.com/studio/write/draw9patch.html
This is good tool:
Click here.
9 Patch images are stretchable, repeatable images reduced to their smallest size. The simplest example would be if you were to take a rounded div and slice it up into 9 squares like you would a tic-tac-toe board. The four corners wouldn't change sizes at all but would be static while the other 5 pieces would be stretched or repeated to allow the whole image to scale appropriately.
With that explanation and the advent of CSS3 you might think that there is no reason to use 9 patch images but the name '9 patch' is a misnomer. The images can be sliced up into even smaller pieces.
9 Patch images contain an index of which piece is what by adding a 1px border to the image. The colors in the border determine if a piece is static (doesn't scale), it stretches, or it repeats.
Google Slideshow: https://docs.google.com/present/view?id=dc7ghz8w_34f8338rcg
See also the Android developer info about 9-patch images: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
Step 1, Since you are not familiar yet, prepare a xxxhdpi png big image to play around first.
Step 2, That image desired expandable field must shorter (by removing redundant/repeated color part) as possible since 9-patch no such thing "reduce", but "expand" the image.
I personally use the ImageMagick command line tool to convert it, e.g.:
convert -trim 'my_image.png' png32:my_image_trim.png #trim extra transparent surrounded image
rm lala*; convert my_image_trim.png -crop 310 +repage +adjoin png32:lala%02d.png #cut redundant/repeat center part
convert +append lala02.png lala05.png png32:out_right.png #append left/right images side by side
convert -resize 144x144\! out_right.png png32:my_image.png #resize to the desired dpi
There is one pitfall when convert: I must prefix with png32, or else will get black 9-patch image, see this thread.
Step 3, Copy image to Android Studio drawable, then right-click and choose menu item "Create 9-Patch file...". A .9.png new image will be generated with the same image name. Now can just delete the original image. Careful when playing around with refactor to rename for backup since it will rename the XML image id too, and make you wonder why the 9-patch image not working since XML still referring non-9-patch image.
Step 4, Left and top 2 black lines form an expandable rectangular area, while right and bottom 2 black lines form a text rectangular area.
Your image size will grow on your declared expandable area as text growing. While the text area means text only allow in that area.
You don't have to draw the black dot/lines from scratch for simple usage, 4 black lines already existed behind the top, left, bottom, right on that image if your 9-Patch image generated by Android Studio. "Zoom in" to bigger if you can't see to drag that lines.
The original position of 2 black lines, vertical and horizontal, both declare an expandable area:
The original position of left vertical line is between upper left to bottom left
The original position of top horizontal line is between upper left to top right.
The original position of 2 black lines, vertical and horizontal, both declare a text area:
The original position of right vertical line is between upper right
to bottom right.
The original position of bottom horizontal line is
between bottom left to bottom right.
Above is the original position of black lines before you start to drag to narrow down the length, to adjust the start position and end position of that lines.
Both expandable and text areas can be different depends on your needs. But normally expandable area should equal OR less than text area, a classic example will be chat bubble image:
The image above has equal top and bottom black lines width but right black line is higher than left line, which also means that text consistently stay up to half of bottom curve in either minimum size or expanded size. And it only expand in body of text area.
Now you will know the two benefits of 9-Patch: the bottom and right line together form a text area which perfectly ensures text never overflow to outside of image curve ! And also defined which portion of text area responsible to expand the image as text growing, while keep the curve without scaling.
Hover the line will able to see the x,y positions in pixels, it help to measures both lines are equal or less in position.
You should remember the position of line when dragging, since left line can drag to right side, and the right line can drag to left side, and you may get lost of which line is expandable line and which line is text line.
There are 2 important checkboxes you should tick, i.e. "Show patches" and "Show bad patches" checkboxes.
In "Show bad patches" checkbox, if you drawing the expandable area but covering the curved line of your image instead of a straight line, then it will mark that area as red to warn you. You can narrow your line to dismiss that red warning, or just ignore it. Keep in mind that, the red warning may misleading which may actually be caused by the opposite line, in that case, you need to narrow the opposite line to dismiss that red warning.
Step 5, In xml, you can refer to that image as TextView background like below, use wrap_content to make it expandable:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_9_patch_image_name_excluded_.9"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<2nd TextView />
</LinearLayout>
When first time playing around with it, ensures no padding on textview or fancy parent layout, or else you may wonder why not working as expected.
Button background example:
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/your_9_patch_image_name_excluded_.9"
android:layout_gravity="center"
/>

Android: How to set a background image to a button which is given "wrap_content"

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.

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