I'm having problems with images on android, I need to create one image that have the same size in different devices but I don't know how I calculate this using DisplayMetrics.
How can I set the same size in different width and DPI devices?
Short answer: use an amount of dp (density independent pixels) as width and it should look the same on different devices.
To get a better understanding of screen sizes I would recommend reading this article from Google Developers https://developer.android.com/guide/practices/screens_support.html
I think you mean using "in" (inch) instead of dp/dip and worth to know those differences between measurements in android from this question
If you want to display the image in same size in different devices, perhaps you should use Imageview for that, it will show you the image exactly in same size and position in every device.
Related
In my activity, I use DisplayMetrics to dynamically get the pixel height and width of the screen, and then I assign each of the components in the Activity sizes based on those dimensions. I wanted to know how this could be affected by screens that have different densities? Is it a good idea to use pixels?
Edit:
The purpose of using pixels dynamically is so that my layout scales based on the given screen. I just want to know how density will play into this. For example, if I have two screens with a height of 1024px and width of 800px, but one is twice as dense as the other, and I want to use 40% of the height and 40% of the width (this is just hypothetical) for a button, why should the density matter? This will just mean that the size of the button will have more pixels in the higher density screen, but the physical size of the button will be the same as DisplayMetrics will always give me the absolute size in pixels. Or am I wrong about this?
See this question and its answers. You will get the answer to your question.
Edit:
From one of the answers on the mentioned question
If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.
Screen Sizes and Densities
To help in your case it is proposed to use dp units instead of pixels, but still there will be differences from one device to another.
On a tablet screen with a high pixel density, the elements probably will occupy less relative space.
If you want to improve it more then you will have to do the dimensions calculation by your own.
Or use a layout that auto distributes the space, for example the LinearLayout
Also you have to take into account that it is the system that decides the size of some widgets, for example the standard buttons
i want a default pixel size of image in this size should be adaptable for all type of screen size.Anyone can help me please, How to fix image in all type of mobile screen. it cant be stretched out.
thanks in advance.
There is no default pixel size.
You will either have to design images for each bucket you wish to support, or try and create a 9-patch for your images.
The Supporting Multiple Screens document has everything you need to having your UI scale nicely across devices.
Sorry, You cannot have a Default size...
If you are developing a Mobile app, then you should detect the User's screen size and use an appropriate dimension of the picture there....
Have at least three different variations of the same picture for different screen sizes..
Android Has an inbuilt way of doing this Have a look at this : Supporting Different Screen Sizes
Sorry but i cant understand how i can draw a right picture for the right android phone size.
I readed the android documentation, and they say for i just think in screen size and density and not in resolution, so what size should have my picture?
For example,if i have a phone with size 1000x400(stupid example),and want a button(40x40) that will be in middle,what size should i do?? 40x40?? But in documention they say for dont look for resolution :\
Im confuse...
ps: The documention link Android multiple screens
Basically you'll have to realize that although resolution, screen size and screen density are separate attributes, they are still somewhat related. If your button is 40x40 as you mentioned, and that's the size you find looks good in the center on a hdpi(high density) device, you will have to scale it so that it fits accordingly on mdpi(medium density) and xhdpi(extra high density) devices. What I like to do is use PhotoShop or another graphical editor and resize my assets so that they fit on whatever density devices I'm trying to target. I make sure to always use *WRAP_CONTENT* for my height and width attributes and never fixed values.
Also, if you do not include these scaled alternatives in your res/drawable folders..you're basically saying that you're relying on the system to scale them for you, which can be a gamble. So I always go with resizing my assets so that I include a version for all densities. The link you posed explains everything pretty well
I am developing an android application for resolution 320x240.
i want to use this same application for higher resolution is it possible
or i have to design UI for every resolution
Scale your application to be supported in different screen resolutions by following the guidelines at this link.
http://developer.android.com/guide/practices/screens_support.html#screen-independence
Mainly you have to use density index pixels(dp) as the unit to define size wherever you need rather than hardcoding with px
Android was designed to fit different resolution automatically. I would advice to read article from google about it.
You need not to use different layout for different resolution. What you need is just use three different resolution images of same name for all images and keep them in separate drawable folders(named drawable-hdpi,drawable-mdpi,drawable-ldpi) and also use screen width according to screen resolution whenever needed.For example :
int width=getWindowManager().getDefaultDisplay().getWidth() ;
gives you complete screen width.
For more details see Supporting Multiple Screens
Hope this will help you.
Make your design 'fluid' by avoiding e.g. AbsoluteLayout (see the documentation for details). Graphics should be provided for each resolution in the drawable-Xdpi folders.
I developed UI for iphone apps and now want to use the same UI in Android apps. I read that Android use dip for image resolution and i also read that 1 dip=1.5 pixel.I simply multiply the image size by 1.5px. Now the problem is that the image is blur and not as clear as in iphone apps.So will some body suggest me how should i make a design so that it could be used in iphone and android.
dip is not simply the same as 1.5 pixels. A dip is a density independent pixel, and allows you to size items in a way so that they will look similar on displays with different pixel densities. When performing the layout Android scales the dip value depending on the pixel density of the device.
However, dip works best when sizing items which scale well, like the Android widgets. If you have an image you may want to always display it "actual size" so the image doesn't get scaled and become blurry - i.e. size it in pixels and not dip. So what you may have to do is supply a number of versions of each image so that you have a version that looks good for the screen densities on all the devices you choose to support. This is turn may mean producing a number of different layouts, depending on how you use your images.
I think it is not a big problem.You just copy your all images from drawable-hdip from drawabel-mdpi.It will work fine.