Android xml sp, dp , px issue - android

I know there is a lot of stuff related to how does android store images. and what does mhdpi xhdmi lhdpi etc means
But my question is a bit different. actually i am working on a screen whose height and weight in px is 720*1020 px. this is not complete HD, this is sort of SD screen. now i designed images for 720*1080 px with size equal to 520*370 px this is the image size. and i have stored the image in xhdmi
The Issue is
When i put the image on screen and give it
android:layout_height = "wrap_content"
android:layout_width = "wrap_content"
the image is displayed on the screen but it shrinks a to half the original size, so i tried giving the height and width equal to 520px and 370px respectively to the layout then i get the original size of image
My question is:
What size should my image contain so that if i wrap content the height and weight, it gets the original size.

In your case the image is resized based on the screen density of the display. Because you placed the image in a higher density folder than your phone is using it will be downscaled. The forth response from How to resize the bitmap image according to mdpi,hdpi and screen size of the mobile explains how the images scale depending on screen density.
You can consider the density on your device, the size you expect to be and increase the size with a multiplier so you can save it in xhdpi folder.

Related

What is the resolution of default layout in Android?

I am just using simple default layout and not adding any of these layout versions; l-dpi-Layout,m-dpi-layout etc.. then what exactly the resolution of my default layout is?
Let say i add an image and define its width-height as 20dp X 20dp. This image is looking perfect in layout Preview screen, but would it look same in 1440X2560 screen or 1920X1080 screen.
My concerns are:
what exactly is the resolution of default layout is?
How would android know that the 20 dpi i am defining fits best for 1080X1920 resolution and 27dpi fits best 1440X2560 screen, when i am writing 20dpi in my default layout which perfectly fits in layout preview screen
The system assumes a target device with a density of 160dpi. When you define the size of yout image with dp unit, Android adapts the size according with the resolution of the device. For example, if you put an image of 100dp x 100dp in a 160dpi device it will take 100 x100 pixels, but in a device with a 640dpi screen it will take 400 x 400 pixel
By default when you create a template app, your main content layout has two parameters values as layout_width=match_parent and layout_height=match_parent. match_parent value means whatever the screen dpi size the mobile has, it stretches all the way to its size of width and height respectively which is calculated by android automatically when it runs its layout pass to position views (including padding). So in this context the default resolution of your layout is what your mobile has minus the size of statusBar and navigationBar.
Android translates the DPI value you enter for your width and height into number of pixels internally to size your views. You can also put the size of your view into number of pixels but its not recommended.

form top image resizes and don't stable in all screen sizes

i'm working on application that have top image in all forms.
i designed the image correctly but to be in top i should set scale type= "fitstart" to be truly be in top.
but the problem is when screen rotates or when app run in bigger screen the image wont stretch in horizon. i think my problem is the imageview size. and it's not just in size of my image and it have blank top and buttons.
how i can solve this problem and what should be the image property. with regards
For this you need to maintain your images in different folders in
res/drawable-mdpi/your_image.png // bitmap for medium density
res/drawable-hdpi/your_image.png // bitmap for high density
res/drawable-xhdpi/your_image.png // bitmap for extra high density
small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
And make sure "adjust view bounds"=true in your xml
follow this for more deatils

Converting images to DPI (Dots per Inch) sizes

I am having a big trouble in resizing my images for Android image sizes, ldpi (120 dpi), mdpi(160 dpi), hdpi(240dpi), xhdpi(320 dpi) and nodpi using Photoshop, because I don't know how to convert my images to these size. As you know, Photoshop has no DPI stuff to work with. So, what I actually need is to convert my images to ldpi,mdpi,hdpi and xhdpi sizes using Photoshop.
So, how can I convert these images to the Android deserved sizes?
Have you tried using Illustrator? The psd files can also be worked with it.
You could also use Inkscape or other free tools.
Check this: http://envyandroid.com/archives/271/easiest-way-to-create-android-icons
Hope this helps.
The answer is right there in your screenshot. Photoshop uses Pixels/Inch which is the same as DPI (well mostly, anyway).
To achieve what you are trying to do, simply alter the Pixels/Inch in the Image Size dialog to your DPI value - start with 320dpi, since shrinking images will look better than enlarging them. After altering the value you will notice that your width and height values (Pixel Dimensions) are changed, change them back to the desired resolution for your 320dpi image (make sure that you use a sufficiently large resolution for 320dpi).
Now create your image for this resolution. Once you are complete, open the Image Size dialog again. Now all you need to do is alter the Pixels/Inch value to the remaining DPI values (240, 160, 120) and your image will be scaled down (notice how the width and height for Pixel Dimensions will get smaller with each decrease in DPI).
If you have no idea about the starting resolution you can use the width and height under Document Size to set the actual resolution. For example, the Nexus 4 is a 320ppi device (mostly) and has a screen size of 4.7" (5.27x x 2.70x), so if you use a width of 2.635" and a height of 1.35" it should cover 1/4 of it's screen (half of width and half of height).

How to resize the bitmap image according to mdpi,hdpi and screen size of the mobile

I am using image say 85x85 px (putting this image in drawable-mdpi)
and if i am displaying this image of 85x85 px on [320x480]mdpi screen size device it looks good,
but while displaying this image on [480x800]mdpi device it looks very small.
I wants to know how can i resize this image of (85x85 px) so that it works fine for the device having screen width and height of 480x800, mdpi.
You have to set the image size in the xml in dp. This post is really helpful.
Consider MDPI image(let say 85x85) as baseline Create images as follow
FOLDER ImageSize Percentage
LDPI 64x64 75% of baseline
MDPI 85x85 100% BASELINE
HDPI 127x127 150% of baseline
XHDPI 170x170 200% of baseline
The better solution for this is to produce LDPI,MDPI,HDPI,XHDPI,XXHDPI sized images and place it inside the corresponding folders.
Put the image in /drawable, and define the size of the image in your XML as 85dp, it will then be scaled for LDPI/HDPI/XHDPI/XXHDPI and god forbid XXXHDPI.
However, the better solution would to produce HDPI(128px)/XHDPI(170px)/XXHDPI(255px) version of the graphics and put them in /drawable-hdpi, /drawable-xhdpi, /drawable-xxhdpi respectively. This way you can provide the best experience for your users.
[Edit]
"dp" is Android's way of defining physical size of an UI objects, so a button of 48x48dp will have roughly the same physical dimension even when run on screen sizes between 240x320 to 1080x1920, for more information check Android developer site's Supporting Multiple Screens.
[Edit 2]
For images on canvas you can use this to calculate the scaled size of the image:
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int newSize = (int)(85 * (dm.densityDpi / 160f));

Why my 720 px image got distortion in my 720 px galaxy S3

My question is just in the title.
I try to put an image to my layout, i tried this with a FrameLayout's background with wrap_content width and height and also tried with imageView with all of the possible fitScale properties.
I just cant see why my 720 px width image is HALF on the width of my phone's screen which is a samsung galaxy s3 with 720 px width....
My real question:
What is the best way to ensure that my pics in applicaion wont get any distortion?
First of all, you need to think in screen densities and dip values, not pixels. That means that you are dealing with a "virtual screen" where 1 dip (density independent pixel) represents a different amount of real screen pixels depending on the device.
For drawables you have the following categories for getting crisp results on any screen:
ldpi (Scale factor 0.75)
mdpi (Default / baseline: scale factor 1.0)
hdpi (1.5)
xhdpi (2.0)
Create different sized versions of your bitmaps based on these scale factors to get crisp results on any screen. You should start from xhdpi as highest resolution to avoid quality loss because of upscaling.
Put these different versions into their respective drawable folders (res/drawable-ldpi, res/drawable-mdpi...). These different versions of one and the same bitmap must bear the same file name of course, otherwise that won't work.
Second of all you shouldn't make strong assumptions about the device screen height and width. You need to layout your views and graphics so they dynamically make use of the whole screen not knowing the exact screen resolution. However, you can make weak assumptions about these screen resolutions based on the configuration qualifiers
small
normal
large
xlarge
Try to avoid fixed sizes for your views based on any assumptions. Always avoid "px" values in layout XML files. Use "dip" instead.
Read the documentation Supporting Multiple Screens for more information.
What you want to achieve sounds as if you want your image to take up the full width of the screen.
If you set the image as a background of any view (FrameLayout for example) then the displayed clip of the image depends on the size of that view.
If that view has layout_width / layout_height set to wrap_content then the size of it depends on the dimensions and arrangement of its child view(s).
In case of FrameLayout it depends on the size of that single child of FrameLayout.
If you're using ImageView the image will be scaled to fit into the size of the ImageView by default. The size of the ImageView depends on its layout_width / layout_height values. If you set these values to match_parent and if the ImageView has enough room "to grow" you should be able to see your image stretch all over the screen.
The "room to grow" depends on the ImageView's context in the layout. Does it have neighbors that take up room? Is the parent view too small (because of wrap_content for example)? Look for these possible problems.

Categories

Resources