So I've patched an image however it has padding on the left and right which I DO NOT want, however when I patch the image without padding the corners on the image get distorted when it's drawn in a layout.
Below is a screenshot of how I am doing it, how would you overcome this issue?
Maybe just try to specify the padding the way it's meant to be specified, i.e. by drawing the bottom (and right if you want) border in 9-patch tool. Like this:
This way the padding should be right (side to side) and corners not distorted.
The left and top lines define the stretchable area. When you include your corners in the stretchable area, the image gets distorted. In this case, one black pixel touching the center of the image on the left and the same on the top would suffice.
To identify the rectangle for the view contents, and thus the padding, you can use right and bottom lines.
Related
I have this image http://i.stack.imgur.com/i8Iab.png
I want to preserve the circle at the bottom of the image so I make this 9-patch
http://i.stack.imgur.com/UNWdE.png
but somehow in the mobile the circle becomes ellipse like this
i don't know why the bottom part get stretch.
Any help please, Thanks.
Because the 9 patch you created is wrong.
And unoptimized (you can make it much smaller): the upper black line should leave a transparent hole corresponding to the circle.
The black lines result in a stretched area, while the parts you leave out aren't stretched.
So, just leave the area you don't want to stretch transparent.
This is the reference site
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 have a few issues and questions regarding some UI development on Android. Firstly look at this image:
Firstly at image A) This is a image that I insert and repeat horizontally to give the zig-zag shape at the top of the screen regardless of screensize and above that a textField with no content and a background color - But as I'm sure you can is that the two colors don't match up quite as perfectly as it should even though they both have the same hex color value (#BF0426). And secondly, you can also see some serious pixelation on that zig-zag image. How do I fix these 2 problems?
And then at image B) Here I have a simple listView. What I would like to do, is firstly indent it a bit on both sides, and round the corners of the top and bottom item. So basically I want it to look like the blue border drawn on the screenshot. (Your typical iOS listView). How do I go about doing this?
Thanks in advance for any tips!
For the image. Just out of curiosity, why wouldnt you simply extend your image to include the top pixels as well? In other words, build your repeating image such that it incorporates the pixels that you are trying to create using the empty textview. This will at least take care of the color missmatch, as the color will all be generated from the same place. In terms of the pixelation. Are you truly repeating the image, or are you spreading the image. A spreading will definitely cause what you are seeing.
For your tableview:
For the left and right indent, you can simply use the margin or padding attributes of tableview item. There are generic padding and/or margin attributes (which will pad all of the top, bottom, left and right), or there are separate padding and/or margin attributes for each top, bottom, left, and right. Here is a great link on padding versus margin that you may want to read.
Difference between a View's Padding and Margin
Example attribute (as called in an XML file) for bottom margin
android:layout_marginBottom
setMargins(left, top, right, bottom) // for setting margins programmatically
Example attribute (as called in an XML file) for top padding
android:paddingTop
setPadding(left, top, right, bottom) // for setting padding programmatically
For the custom top and bottom, you should be able to use the following tutorial
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
From here, you should be able to detect the item as the first and last item and set the background property (image) to a background image that has a rounded top or rounded bottom. This is how I do it on the iPhone. I have also implemented things that LOOK like tableviews but are simply vertical layouts with my own custom views that I have made to look like what you are wanting.
One thing to consider is the file format you use is going to mutate the image as you save it (lossy compression), so a .jpg with a certain color may look different than a .bmp with the same color. Make sure you author using the same application and/or export settings when trying to match up images.
Also, if you created your image with a DPI that is not compatible with the android display it can cause serious pixelation in detail areas, especially with gradients and drop shadows.
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.
In a 9patch png, there are black edges in its four sides, but there is usually difference between the left to right, and the up to down side. Why? How does this difference impact?
One of simplest ans is
draw the black line according to your requirement using nine patch tools.
The left and top are for scaling, and the right and bottom are for content. So the black lines on left and top will scale to fit, and the content for a button will be "inside" the black lines on the right and bottom.
Here http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch you can find a very good explanation, with images to show you that the left and top part of the black border indicate a stretchable section, while the right and bottom lines define the drawable area, where you can put your content.