In my application there is a background and a picture on it. By default, I only have a drawable folder in AS. Created xxhdpi, xhdpi, hdpi, mdpi in the res directory, scattered on them smaller copies of the image and background. I understand that the application itself should take a picture from a specific directory, depending on the screen resolution. But I don’t even understand how and to which picture to apply in the code. Please help me figure it out.
- All copies of different sizes should have the same name. If you contact the id generated by the R-class for this name, the system will choose the most suitable size based on the current system configuration. - pavlofff
- Tags (tags) are needed to highlight the main points of the QUESTION, according to which other users will be able to find a solution to a similar problem more quickly, and not to demonstrate their own preferences in choosing an IDE. The question has nothing to do with the problems with the work of IDE Android Studio and this tag is not needed in the question - pavlofff
2 answers
To get started, you need to familiarize yourself with the official guide to support devices with different screen sizes . It explains in great detail EVERYTHING on this topic.
In order for the system to use a specific image for some kind of screen density, there are two ways:
Manual.
You need to create in the / res directory of the project the / drawable folder with qualifiers of the desired density, for example, to enable HDPI, specify the -hdpi: / res / drawable-hdpi qualifier (a full list of possible qualifiers is listed in the table ). In this folder to place the image for the specified density with the same file name, the folder structure will take the following form:res/drawable/ image1.png res/drawable-hdpi/ image1.png res/drawable-xhdpi/ image1.png
When you run the program on a specific device, the system will determine what the pixel density of this device and will use the image from the folder with the appropriate qualifier. If there is no suitable qualifier, the resource with the most appropriate density value will be selected or from the / res / drawable / folder , if there is nothing better (no density qualifiers are specified).
When building the project, the IDE will generate a resource file R, which will contain links to all the resources of the project. Access your image from the code as follows:
R.drawable.image1 The system itself will select the most suitable option for the current device.
- Auto.
IDE Android Studio has a tool for automatically preparing images for different screen densities.
Click on the res / project folder and follow: New -> Image Asset . The Asset Studio tool opens, where you need to:
- select the type of image being prepared (icon for the application, action bar or notification).
- specify an image file from which to prepare a set for the required densities (Image checkbox), or choose from default images (Clipart checkbox)
- Specify the name that the resource will receive in the project.
- You can also set paddings, trimming and the shape of the substrate (square, circle, etc.)
Pressing the Next button of the wizard will take you to the next screen where you need to choose which folders to create for individual densities — select the folders you need using the left click while holding the Ctrl (Shift) button.
Clicking the Finish button of the wizard will automatically prepare the images for the indicated densities, create the necessary folder structure and place the corresponding images in them.
The disadvantage of this tool is that it is impossible to process an arbitrary image like that. It works only for the specified types of icons and creates only files of the size corresponding to these icons, for example, an image with the size of 72x72 pixels will be created for the icon of the application under HDPI density.
See also these answers for related questions: answer1 and answer2
Created xxhdpi, xhdpi, hdpi, mdpi in the res directory
First, there must be a folder called drawable-hdpi , drawable-mdpi and so on ...
the application itself should take a picture from a specific directory depending on the screen resolution
Yes. That’s what special folders are needed for each resolution.
how and to what picture to apply in the code
For resources located in the res folder, the class R automatically generated. Access to these resources occurs through it.
