博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Supporting Different Densities 支持各种屏幕密度
阅读量:4046 次
发布时间:2019-05-24

本文共 2094 字,大约阅读时间需要 6 分钟。

Use Density-independent Pixels

One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use eitherdp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. Ansp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).

For example, when you specify spacing between two views, use dp rather thanpx:

When specifying text size, always use sp:

Provide Alternative Bitmaps

Since Android runs in devices with a wide variety of screen densities, you should always provide your bitmap resources tailored to each of the generalized density buckets: low, medium, high and extra-high density. This will help you achieve good graphical quality and performance on all screen densities.

To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:

  • xhdpi: 2.0
  • hdpi: 1.5
  • mdpi: 1.0 (baseline)
  • ldpi: 0.75

This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 forhdpi, 100x100 for mdpi and finally a 75x75 image for ldpi devices.

Then, place the generated image files in the appropriate subdirectory under res/ and the system will pick the correct one automatically based on the screen density of the device your application is running on:

MyProject/  res/    drawable-xhdpi/        awesomeimage.png    drawable-hdpi/        awesomeimage.png    drawable-mdpi/        awesomeimage.png    drawable-ldpi/        awesomeimage.png

Then, any time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen's dpi.

转载地址:http://wagdi.baihongyu.com/

你可能感兴趣的文章
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
一文看清HBase的使用场景
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>