当前位置:网站首页>21 Days Learning Challenge - Day 1 Punch (Screen Density)
21 Days Learning Challenge - Day 1 Punch (Screen Density)
2022-08-02 10:40:00 【Meng Fangfang】
1. Recall
I have been engaged in Android development for a few years. Looking back now, I was very happy when I first started to display a few simple controls, then I came into contact with the four major components of Android, then I customized View, and finally I used a third-party open source framework.Do a few functions that I think are cool... and then it seems to stop, and for several years in the middle, I "wasted my life" with this basic thing...
It wasn't until the first two years that I realized that it couldn't go on like this. If I just knew about the ever-changing technology, but I didn't know why, it would soon be eliminated, so I started to study the source code.As soon as I researched the source code, I found that I didn't seem to know anything, and I was in the confusion of just entering the industry...
Take advantage of the 21-day learning challenge opportunity, force yourself to learn hard for 21 days with the great God!
Day 1: Pixel density, screen density, resolution.
2. Background:
In order to adapt to different screen sizes, during the Android development process, you need to add multiple pictures of ldpi, mdpi, nodpi, xhdpi, xxhdpi, xxxhdpi to the resource file.
3. Several concepts
①Screen size
Screen size refers to the physical size of the phone diagonally, in inches (inch), one inch is about 2.54 cm.
General mobile phone sizes are 4 inches, 4.5 inches, 5.0 inches, 5.2 inches, 5.4 inches, 5.99 inches, 6.0 inches, 6.2 inches, etc.
②Screen resolution
Resolution is the total number of pixels on the screen of a mobile phone. Generally, the number of pixels in the screen width is multiplied by the number of pixels in the screen height.The larger the resolution, the more delicate the screen and the more details that can be displayed.
Commonly used resolutions are 320x240, 640x480, 1280x720, 1280x960, 1080x1920, 2560x1440, etc. The unit is pixel.For example, 1080x1920 means that there are 1080 pixels in the width direction of the screen and 1920 pixels in the height direction of the screen.
Get screen resolution:
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // screen width
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // screen height
③Pixel density
Pixel density (dpi, dots per inch; or PPI, pixels per inch) refers to the number of pixels per inch.
④Screen density:
Screen density is actually another representation of pixel density, based on 160dpi=1.0.After the mobile phone leaves the factory, the screen density, including the pixel density in the X and Y axis directions, is a fixed value.
Android divides the screen based on the pixel density of 160dpi. When the pixel density is 160dpi, the screen density is 1.0, when the pixel density is 120dpi, the screen density is 0.75, and when the pixel density is 320dpi, the screen density is 2.0.
Android divides the actual screen density into low, medium, high, and extra high, extra extra high.
General screen: ldpi is 120dpi, mdpi is 160dpi, hdpi is 240dpi, xhdpi is 320dpi, xxhdpi is 480dpi.
Get pixel density and screen density:
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // pixel density (pixels per inch: 120/160/240/320)
float xdpi = dm.xdpi; //Pixel density in the X-axis direction
float ydpi = dm.ydpi; //Pixel density in the Y-axis direction
screenWidth = dm.widthPixels; // screen width
screenHeight = dm.heightPixels; // screen height
4. How to load multiple drawables
When the android system adapts to the drawable, it will first look in the dpi directory corresponding to the device.If it is not found, it will follow the principle of "first high then low", and then scale the image proportionally.
For example, it is currently an xhdpi device (there are only xxhdpi, xhdpi, xxhdpi, nodpi, mdpi, hdpi in the project), then the search sequence for drawables is: first search for the xhdpi directory, if not found, then search for xxhdpi, if not found, thenFind xxxhdpi, find nodpi if you haven't found it, find hdpi if you haven't found it, find mdpi if you can't find it, and search in turn.If the target image is found in xxhdpi, it will be compressed by 2/3 for use (because the system thinks it has found an image larger than the appropriate size), if the image is found in mdpi, it will be enlarged by 2 times to use (the system thinks it has found a picture)A picture smaller than the suitable size needs to be enlarged to ensure normal).
边栏推荐
- 利用二维数据学习纹理三维网格生成(CVPR 2020)
- Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
- 21天学习挑战赛--第一天打卡(屏幕密度)
- 众城优选系统开发功能
- After 21 years of graduation, I switched to software testing. From 0 income to a monthly salary of over 10,000, I am really lucky...
- MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
- Oracle根据时间查询
- MySQL模糊查询性能优化
- 从众多接口中脱颖而出的最稳定的接口——淘宝详情api
- 字节跳动软件测试岗,收到offer后我却拒绝了~给面试的人一些忠告....
猜你喜欢
随机推荐
配置mysql失败了,这是怎么回事呢?
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
Three.JS程序化建模入门
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
The realization of the list
win10打印服务无法启动(运行时错误automation)
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
Geoffery Hinton:深度学习的下一个大事件
5G基础学习1、5G网络架构、网络接口及协议栈
org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
idea常用插件
从零开始Blazor Server(5)--权限验证
currentstyle 织梦_dede currentstyle属性完美解决方案
LayaBox---TypeScript---模块
Event object, do you know it well?
你好,我的新名字叫“铜锁/Tongsuo”
详细总结SoC、DSP、MCU、GPU和FPGA等基础概念
ASP.NET Core 6框架揭秘实例演示[31]:路由"高阶"用法
3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...
程序员的浪漫七夕