当前位置:网站首页>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).
边栏推荐
- 周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
- org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
- 软件工程国考总结——选择题
- Event 对象,你很了解吗?
- leetcode: 200. 岛屿数量
- DVWA Clearance Log 2 - Command Injection
- Hongxing, donate another million
- WPF 截图控件之文字(七)「仿微信」
- STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
- 有奖征集|TaoCloud&宝德联合举办全闪POC!
猜你喜欢

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...

通过方法引用获取方法名

字节跳动软件测试岗,收到offer后我却拒绝了~给面试的人一些忠告....

2022年8月初济南某外包公司全栈开发面试题整理

AlphaFold又放大招,剑指整个生物界!

Linux system uninstall, install, upgrade, migrate clickHouse database

深度学习100例 —— 卷积神经网络(CNN)实现mnist手写数字识别

MySql模糊查询大全

超赞!发现一个APP逆向神器!

Linux系统卸载,安装,升级,迁移clickHouse数据库
随机推荐
LayaBox - TypeScript - merge statement
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
你好,我的新名字叫“铜锁/Tongsuo”
从零开始Blazor Server(5)--权限验证
从零开始Blazor Server(5)--权限验证
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
为什么要使用BGP?
【面向校招】Golang面试题合集
LayaBox---TypeScript---装饰器
后管实现面包屑功能
同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了
鸿星尔克再捐一个亿
STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
WPF 截图控件之文字(七)「仿微信」
Hongxing, donate another million
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars