Wrap_contents or DP for ImageView - android

I see some people user the exact dp measurements for Imageview width and height in their layout while some others use wrap_contant.
Wrap_content would take the image size and display it accordingly. So if it is 60px by 60px then it will look smaller on high density screen and larger on low density screen.
While using the exact dp measurements lets say 60 dp, will make it look of the same size on all the screens.
As a result, shouldn't we be using the exact measurements all the time and not wrap_content? Am I missing something here
Thanks

I think if you use wrap_content, then the system will choose the resource from the appropriate resource folder depending on the screen density (mdpi, hdpi, xhdpi, etc) and will use the px dimension of that resource. Since the screen density resource folders (mdpi, hdpi, etc) can each apply to various screen densities (ie, mdpi is ~160dpi, but can be a bit less or a bit more before hitting the next density), this means that the image may change size slightly on different screens. If you specify the actual dp value for height and width, then you ensure the image will scale to maintain the same physical size on all devices.

Sometimes wrap_content is too small than what you want, or too big than what you want
so you can give exact measurements
If you want to make sure your exact measurements support multiple screen sizes, you can put them inside a dimens file which then loads according to the correct screen size
look into Support Multiple Screen Sizes
if for example you create a folder calld "values-sw600dp"
and put a dimen file in it, and have a property
<dimen name="imageSizeWidth">60dp</dimen>
And your image will be defined as follows
<ImageView
android:layout_width="#dimen/imageSizeWidth"
android:layout_height="#dimen/imageSizeWidth"
/>
Then it will be loaded, if the device matches the description "at least 600dp scree width"
this is providing there are other values folders in your resources.
If you don't have any other values folders, then this will be loaded by default

Related

If our .png drawable images use a constant DP size, does it matter if we supply differnt .pngs for each screen size?

Basically title. In my layout, my images use a constant size of 200dp x 200dp. So does that mean I shouldn't bother including drawable-hdpi, drawable-xhdpi etc. as long as I use a single 200 px by 200 px image?
200px by 200px is not the same as 200dp * 200dp
Read: https://www.quora.com/What-is-the-difference-between-px-dp-dip-and-sp-in-Android
What i mean by this, is that its impossible that your image is 200dp * 200dp in every screen.
Since dp's size change in every screen, For some screens [160 dpi screens only] 200dp * 200dp is 200px * 200px, but in higher dpi screens this isn't the case, this is why if you only supply a 200px by 200px image it will look perfect in a 160dpi phone screen but in a higher dpi it will look bad (because it will look stretched.) since the higher the dpi the more pixels a dp is.
Here is a pixel to DP calculator so you can check it out for yourself.
https://pixplicity.com/dp-px-converter
That being said if you only supply one xxxhdpi image it should look perfectly fine in every screen but it takes more memory to load that image since it naturally is a higher resolution.
So if you load a xxxhdpi on a mdpi screen it will look worse than the image itself because the resolution for that phone doesn't support it but it will keep the highest quality possible for that screen.
This is why its usually best to have many images for many cases.
Example below of what is 200DP in pixels across different screens, as you can see 200DP can be from 150 pixels to 800px.
Thats why what you're suggesting of having an image in a constant DP size is impossible unless you have an image for each dpi.
Simple answer: NO.
The best way when you have only one picture for all resolutions is to get a better resolution png and you put it in drawable-xhdpi, or even more. By this way, your image will be well displayed.

scaling factor for sw-XXXdp qualifier

is there any scaling factor for using in sw-XXXdp? i want my exactly look same in different screen sizes and densities.
for example for sw-480dp my value is 100dp what is it in sw-800dp? 100*?
Actually, sw-XXXdp is not a density qualifier, so you will not have any scaling taken in account, except if you combine it with a density qualifier (hdpi, xhdpi, etc).
As found on Android developer's website:
The smallest width qualifier specifies the smallest of the screen's two sides, regardless of the device's current orientation, so it's a simple way to specify the overall screen size available for your layout.
The smallest width qualifier (sw-XXXdp) is just here to make sure that the screen will have a minimum size for your layout. For instance, it's good to use sw-480dp if you have a content that should be at least 400dp wide or high, so you still have space to add margins and have a good look and feel.
In the end, to your question:
for example for sw-480dp my value is 100dp what is it in sw-800dp? 100*?
I would answer that you can follow proportional rules, so assuming you have 100dp as a baseline (mdpi) for a sw-480dp screen, you should have around 166dp for a sw-800dp screen and then adapt regarding the different densities.

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.

larger imageview for large screen size

If I have an image that I want it to be medium on android medium screen and large on large screens. I know I have to put layouts in 2 different folders: medium and large layout.
But my question is, should I use one image (say the medium size) and stretch it for the alrge screen by putting min width, min hight and fitscaleXY.
OR
Should I create 2 images, one for medium sized screen and larger version for large screen?
Thanks
EDIT:
Really no answer yet to this? I am sure this is something people would do all the time. You want the image to be larger on large screens, how do I go about it? I was thinking of specifying width and hight for each screen layout size instead of wrap content.. You agree?
You dont have to make different layout xml files for each screen size, unless they really need a different layout, like how things are organised.
In Android you must specify the sizes in dp, and textsizes in sp, which is an equivalent to pixels, only they will be scaled automatically based on the screen density.
Measurement Units.
If you specify some size in dp, it may show up on a large device at for example 300 pixels, and on a smaller device 200 pixels (just an example).
I would Definatedly not hardcode the sizes per screen, since there are a lot of different screens around.

Different font sizes for different screen sizes

In my application I must use a smaller font for the medium density devices. Is it possible to specify that?
You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.
Then, when you are in medium density device it will pick the file in "values-mdpi" folder if exists or in "values" if not, and the same for high density etc...
This same principle applies to al "res" subfolders (drawables, values, etc...)
Specify all your fonts using dips (e.g. 14dp) rather than pixels (e.g. 14px) and you won't need to worry about screen density. Android will scale your fonts (and layout) accordingly.
Edit: Here's comparison of sp/dp from the Android docs:
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, so 160dp is always one inch regardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens.
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
yes, for implementing an universal app which will be working for all resolution.
You should configure multiple drawables & corresponding layouts.
eg.
drawables:-
drawable-ldpi
drawable-mdpi
drawable-hdpi
layouts:-
layout-small
layout-medium
layout-large
then you can change according to your resolution required for Device.
Android supports internal configuration for Density factor of various Screen's resolution.
the device can take itself as appropriate drawable & corresponding layout.
you dont need to adjust any line of code in your src files.

Categories

Resources