When 9patch png images are used, do we need to provide them in xhdpi, hdpi and mdpi format also or only one resolution is enough? Because either way they will resize on resolution... or am I thinking something wrong?
You should use different sizes, otherwise a border on a LDPI device would look much thicker than on an XHDPI device
For the Nine patch image only one resolution is enough because 9-patch image scales the image text according to the layouts...
Nine-patches are widely used for background kind of images: most typically it would be some sort of border and a fill. You should provide only one resolution of 9patch, Android would scale it appropriately.
Related
I am building an android application and I have issues with images.
I use this image:
and here's my result:
As you can see, the first image is pretty clear but not on my android application. The image is blurry.
I tried using an ImageView and ImageButton but same result for both.
Any solution to this?
Why the image quality is blurry?
Simply because the image size and DPI is low. If you use a small images on a large device, the low resolution image will have to expand to the size in the ImageView or ImageButton property (layout_width and layout_height).
Let's pretend we need to use a small device phone like the Nexus One. This has a resolution of 480 pixels by 800 pixels.
If you have an image that is 128 pixels by 128 pixels within 100% scale, you will put 3 images side by side.
Let me demonstrate you with this example. I am using two images:
https://www.iconfinder.com/icons/310718/fiddle_instrument_music_violin_icon#size=128
https://www.iconfinder.com/icons/310718/fiddle_instrument_music_violin_icon#size=512
Here's the test:
Left Side
I use the 128x128 first, then the 512x512 using the layout_width and layout_height with 128xp as the value. (This is not recommended, just for test purposes).
As you can see, they are very similar in quality. Which make sense since both are sized at 128x128 pixels.
Right side
I used the same 128x128 first, then the 512x512 using the layout_width and layout_height with 130dp as the value. Now you can see the difference in quality (look at the strings). With high density pixels, the 128x128 will have to be stretched and the 512x512 will have to be shrunk. This way, the 128x128 will lose quality image and makes it blurry.
Conclusion
Make sure the images you use have a higher resolution and a high DPI and make sure you put them in their respective folders. (drawable-hdpi, drawable-ldpi,
drawable-mdpi, drawable-xhdpi,drawable-xxhdpi).
Perhaps you're inadvertently upscaling it. Try adding android:scaleType="center" to your ImageView's XML code. If it's then sharp, but not displaying at the size you need, you'll need a higher resolution source.
Convert the images to PNG i think have better quality in that picture. And check the size
I am kind of confused about managing graphic resources in Android.
Tried to read this doc but It only confuses me more. Can anyone give me some example of how should I handle the following case?
Lets assume that I have an image in my layout that will be scaled to fill screen width. What image sizes (in pixels) should I produce and what configuration qualifiers (drawable-{qualifier}) should I apply to those resources to cover all major dpi and screen sizes (both for tablet and handset)?
Thanks.
If you want to have an image, that is supposed to fill the screen it is best to use 9-patch images. This way your image can automatically scale to fit the device. Because even if an image has the correct density, the actual screen size can vary. For example a smartphone and a tablet can both be hdpi, but have completely different screen sizes (and actual pixel count).
So the easiest way to target most devices, when it comes to images that are supposed to fill up the full width of the screen is to have a 9-patch image and create ldpi, mdpi, hdpi and xhdpi versions of it. This way the image will automatically be choosen depending on the density and then stretched to fit the device.
The android sdk also provides a tool that helps with creating 9-patch images http://developer.android.com/tools/help/draw9patch.html
I have a an image of a text field that I want to put as the background of my text field. I made a nine slice image so I can change the size of the image but retain the integrity of the corners. I have this image in my drawable xhdpi and I want to use the same image in my hdpi and mdpi. I need to do this for multiple images in my app so I do not want to have to make a nine slice image for each pixel density. When I drag my nine slice into photo shop to change the size of the image the black lines above and to the sid of the text box are now in the image, not a pixel above/left of my image. Is there any way that I can scale my nine slice? Or do I have to make a nine slice for each pixel density?
Rule of Thumb: When you scale your images for each density, watch out for aliasing problems even when the height and width are in integer multiples.
That is: make a nine slice for each pixel density to be safe as your black dots/lines may get cut out.
If you want to use the same image for all of them, just create a new folder in res called drawable-nodpi, and it'll use that same image for all densities with no scaling. That's what I do for the majority of my 9-patches if the borders are pretty thin. If you have borders intended to be thick on an xhdpi device, keep in mind they'll appear much thicker on an mdpi device if you use the same one. If that's not an issue, go for the nodpi approach, in my opinion.
Scaling an image with Photoshop will either remove or add pixels to the image. When it does that it will change the scale/fill guides so they are no longer 1 pixel wide and solid black. To avoid this crop your image to remove the guides before you scale the image then add them back in.
Or you can convert 9-patch
images to the other resolutions using this Google 9-patch tool.
I want to create a ninepatch background file (the background is a gradient image). I would like to know what are the ideal dimensions of the background image?
In addition to that, where should I put the image: drawable-ldpi, drawable-mdpi or drawable-hdpi?
Check your SDK folder inside: /platforms/android-X/data/res/drawable-hdpi
You will notice there are a bunch of 9patches there. I understand that you should provide a 9patch for each type of device. So you should have a 9patch for ldpi, one for mdpi and one for hdpi.
The idea is to make it as small as possible and also take into account that the 9patch doesn't go smaller than the original.
On the other hand, I don't think that a 9patch will be useful when dealing with gradient images.
I would try using a GradientDrawable.
I think its 40dip x 40dip or 60px x 60px.
I am creating an android application in which I want multiple screen support.For these I have used 9 patch images.
But my question is this whether using 9 patch images will be enough for different-2 density's devices or I will need to create different-2 9 patch images for varying densities(like mdpi,ldpi,hdpi).
Thanks in advance
I recently found out myself in the same situation so let me pitch in and expand on what has been said already...
Yes, 9-patch images will scale, that's what they exist for. But if you should use a 9-patch image for all screen densities, that depends on the image really. 9-patch images are more commonly used, for instance, buttons. You can have different sized buttons in your app and a 9-patch image will help deal with them, no matter how you size your button (as long as the 9-patch image is properly created).
But let's say your button design has some really round corners for the hdpi version. You create your 9-patch image without messing the corners but when you look at it in the ldpi version, you'll realize your corners are too big for that low resolution. For this situation, you'll need a different 9-patch image with less round corners, that look better on that resolution.
So, the final answer is, it really depends on your image. If you can create a 9-patch image that looks good in all densities, than fine, use it, as you only need one image to handle all densities. But if it doesn't look good, because of corners, gradients, or whatever, than you'll need one 9-patch image for each screen density.
Hopefully it's clearer now.
From documentation: 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 answer is no. you nine patch will scale between different screen size
Short answer is YES.
Check this:
By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate
physical size on each device. For example, if your application provides bitmap drawables only for
the baseline, medium screen density (mdpi), then the system scales them up when on a high-density
screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the
bitmaps. To ensure your bitmaps look their best, you should include alternative versions at
different resolutions for different screen densities.
from Android Developer Official Doc