I want to set a background image into my app. I tried to set my background image in my xml file with android:background(im using a relative layout) but it seems the image was scaled and doesn't appear to be correct or in its original form.
Let's take an example if I want to display it on a 480x800 screen and only on portrait mode. Should I also produce a background image with a resolution of 480x800?
Thanks
Yes you should. Keep in mind that there are three different directories to put your drawables in (ldpi, mdpi and hdpi drawables directories) put your background images in the appropriate directory according to pixel density and the app will chose the appropriate one to use according to the device it is run on.
Related
I am using Android Studio. I am trying to set a screenshot from my device, as a background image for my activity -
android:background="#drawable/active"
I have my image inside the directory- res> drawable-ldpi> active.jpeg , Which Is the lowest density folder available I am aware of. The problem is, The background looks PERFECTLY crisp and sharp in the screenshot, but when I set it as my activity's background, it blurs.
How do I fix this? I have tried moving the image to the highest density folder drawable-XXXhdpi, but it becomes worse. I have even tried the drawable folder.
All depends on your phone density. You should make few version of your image for each pixel density (but remember that even mdpi is growing older). If you properly resize image for each density your phone should take needed size of background.
I have an application where I use background images.
I don´t want my images to be stretched or deformed when I run my application on different screens with different ratios.
I already have different images for different screen sizes.
Can someone please explain to me how Android Studio handles the image sizes.
How can I make it that the image isn´t streched, but a sector that fits the screen is being displayed?
Android System uses resource qualifiers/filters to load different resources based on current screen configuration.
Now you have different images for different screen sizes, Good!!
the Folder structure would be something like this.
res
-drawable-mdpi
-drawable-hdpi
-drawable-xhdpi
-drawable-xxhdpi
Now when you run your Application on a device with mdpi screen, android loads images which are under drawable-mdpi folder, and when you run your application on a device with screen density of xhdpi android loads images from drawable-xhdpi folder.
Instead of setting the image as a background for my layout I created an image view. There I set the scale Type to centerCrop and it had the wanted effect!
Is there a way to have Android select a different image based upon the screen size? According to the docs and my testing, images must be placed in folders like drawable, drawable-mdpi, drawable-hdpi, etc.
These folders however relate to the screen dpi and not the screen size. The layout folders are used to handle different screen sizes but they don't allow you to store images in them. Only xml files.
So if I run my app on two devices with the same dpi but with different screen sizes, the same image gets selected from the same drawable folder. What I really want is for a larger image to be selected for the larger screen and a smaller image for a smaller screen.
Thanks for your help.
Yea, you are right, this is how works Android, by saving different pictures in particular folders (l,m,h and xh -dpi).
Where is your problem?
You want to keep images for all screen resolutions? Its too many of them.
Can you write more specific what you want achieve?
If you have a problem in layouts.xml, just check:
android:scaleType="..."
You can use 9-patch images. A 9-patch image can be stretched without loosing quality. You may refer to the following link for more information http://developer.android.com/guide/developing/tools/draw9patch.html
i am putting the image in three of the drawable,drawable-hdpi and drawable-ldpi for supporting all type of screen but when i see the output of background image in 240*320 screen resolution the background image is not as clear as origional so my question is where to, means in which folder i put the background image for supporting it all type of screen size and density, maens then my image should not distorted or bluer...
i am using the background image of size:320*480
thanks
make one folder name
drawable-nodpi
put your image in that and use it and dont keep that image in other folders.
the image will not be scaled or stretched.
Anoher Way
the best way is to make 3 different size images and put int drawable-ldpi(240X320),drawable-mdpi(320X480),drawable-hdpi(480X800) folder with the same name.
TO LEARN MORE PLEASE VISIT THIS LINK.
Supporting Multiple SCreens
You can put different same image in drawable-hdpi,mdpi and ldpi.Depending on the density of devices they will take the images from the corresponding folder.
With different devices you should have same image but with different size and density.for You can search in internet the specifications of devices.If density is less than 160 then put image in ldpi,if 160 then use mdpi and if 240 or above use hdpi.Also change the size of image to the screen size of corresponding device.Hope it will help.
I have an app developed for a Droid (phone). In the app, I use:
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
To set the background bitmap in the application. The bitmap is sized 480x800
which is the WVGA screen size of the Droid (without status or title bar).
Two questions:
When I rotate the phone, the Droid goes into landscape mode and the background no longer fits correctly. How do I tell the App it needs to use landscape mode when I'm not using a layout?
Second, when I run this app on the Xoom, it picks the mdpi drawable which is nowhere near the size of the Xoom screen. How do I define a second mdpi drawable that has a 1280x800 size? I think if I were using layouts, I would create a layout-xlarge directory and place a 1280x800 background in the imageview of that layout, correct?
Thanks!
Update *:
Thanks for the great answers! I upvoted everyone.
I updated the code to use drawable-xlarge-port/drawable-xlarge-land for the Xoom and drawable-port-mdpi/drawable-land-mdpi for the Droid.
I'm not using layouts as I needed to draw directly onto the activity canvas and not sure how to use setContentView() from within the class where I extend View and hook into onDraw() in order to do the animation (would need to be a separate posting).
If I can figure out how to rotate the image myself I could indeed do away with the drawable-*-land and *-port directories.
You can target drawables, not just layouts. You want drawable-hdpi-large-long-land for the first situation (assuming you're letting the system handle rotation events and recreate your activity), and drawable-mdpi-xlarge for the second.
Of course it's futile to try to ship with a separate image for every conceivable phone resolution; partially because there's a lot you'd need, and partially because some targets aren't differentiable (the 854x480 and 800x480 phones out there are all hdpi-large-long, for example, despite the 54px difference). Try to use a scaleable image, a stretchable 9patch, or an image you can just fix at the top-right corner (you can define stretch behaviors using an XML-defined bitmapdrawable).
When I rotate the phone, the Droid
goes into landscape mode and the
background no longer fits correctly.
How do I tell the App it needs to use
landscape mode when I'm not using a
layout?
If you alreay detect the rotation, then the Device class is the way to go.
Second, when I run this app on the
Xoom, it picks the mdpi drawable which
is nowhere near the size of the Xoom
screen. How do I define a second mdpi
drawable that has a 1200x800 size?
You can use various image folders dependending on the resolution of the device: ldpi, mdpi, hdpi or xhdpi. You should take a look at the following page for proper support of various resolutions and screen sizes.
That's just about right. You can mix in portrait, landscape, and other modifiers, too. Don't forget to use them with your drawables as well as your layouts (and any other resource directory, for that matter). If it's the same layout and different drawables, then you only need to apply it to the drawable, for instance.
You'll want to look at the documentation for resource directories and make use of the size and orientation directory modifiers.
For example:
drawable-land-mdpi
drawable-xlarge-land
layout-port-mdpi
and such
For more information, see:
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/guide/practices/screens_support.html#qualifiers