当前位置:网站首页>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).
边栏推荐
猜你喜欢
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...
你好,我的新名字叫“铜锁/Tongsuo”
STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
多线程(基础) - 4万字总结
保姆级教程:写出自己的移动应用和小程序(篇二)
MySQL模糊查询性能优化
Linux系统卸载,安装,升级,迁移clickHouse数据库
MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
ASP.NET Core 6框架揭秘实例演示[31]:路由"高阶"用法
C#/VB.NET 添加多行多列图片水印到Word文档
随机推荐
You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
阿里云数据存储生态计划发布,助力伙伴数据创新
软件工程国考总结——选择题
MySql模糊查询大全
通过方法引用获取方法名
如何封装微信小程序的 wx.request() 请求
详细总结SoC、DSP、MCU、GPU和FPGA等基础概念
yolov7创新点
Jay Chou's new song is released, crawl the "Mojito" MV barrage, and see what the fans have to say!
R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
Com多进程通信实现
配置mysql失败了,这是怎么回事呢?
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
链表的实现
sqlmap安装教程用w+r打开(sqlyog安装步骤)
WPF 截图控件之文字(七)「仿微信」
记一次mysql查询慢的优化历程
为什么要使用BGP?
MySQL模糊查询性能优化
One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework