android - [UIScreen mainScreen].bounds.size.width 的 Android 等效项是什么?

标签 android ios

说到这里,[UIScreen mainScreen].scale 的 Android 等价物是什么?

下列等式正确吗?

[UIScreen mainScreen].bounds.size.width = displayMetrics.widthPixels
[UIScreen mainScreen].scale = displayMetrics.density

像这样获取 displayMetrics 的地方:

DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = ((WindowManager) someContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(displayMetrics);

问题是我对 iOS 和 Android 的措辞感到困惑。

这些 UIScreen 值的定义如下:

bounds

Contains the bounding rectangle of the screen, measured in points. (read-only)

scale

The natural scale factor associated with the screen. (read-only)

@property(nonatomic, readonly) CGFloat scale

Discussion

This value reflects the scale factor needed to convert from the default logical coordinate space into the device coordinate space of this screen. The default logical coordinate space is measured using points, where one point is approximately equal to 1/160th of an inch. If a device’s screen has a reasonably similar pixel density, the scale factor is typically set to 1.0 so that one point maps to one pixel. However, a screen with a significantly different pixel density may set this property to a higher value.

我想知道边界的宽度是否相当于 DisplayMetrics 的 widthPixels 以及比例值是否相当于 Android 上的密度值。

最佳答案

[UIScreen mainScreen].bounds.size.width = displayMetrics.widthPixels

正确,屏幕(显示器)的宽度(以像素为单位),这里没有什么复杂的。

[UIScreen mainScreen].scale = displayMetrics.density

不是真的。它们是相似的值,但绝对不相等。

我将尝试解释什么是scale。我们有屏幕分辨率为 1024x768 的 iPad 1,我们有双分辨率的 iPad 3(苹果营销称它为 Retina),我们希望应用程序可以在两种设备上运行。因此,该应用程序可在 1024x768(逻辑点)分辨率下运行,但操作系统会使用比例(iPad 1 上的比例=1.0,iPad 3 上的比例=2.0)转换为每个设备上的物理像素。 例如,当您在逻辑坐标 (1, 1, 20, 40) 中绘制矩形时,在 iPad 3 上它将绘制在像素 (2, 2, 40, 80) 上。

目前只定义了两个值:1.02.0

密度是一个类似的比例因子,但计算方式不同

This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen Note that again it converts logical points (called DIP on Android) into screen pixels.

iOS 规模和 Android 密度之间的区别在于逻辑单位的定义不同。

关于android - [UIScreen mainScreen].bounds.size.width 的 Android 等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13616649/

相关文章:

java - 如何从其他线程修改 UI 线程中声明的变量?

android - FCM 后台通知深度链接不适用于 android

ios - 如何通过编码xcode改变按钮的位置

ios - 使用 Magical Record 导入的记录不会立即显示在 tableView 中

objective-c - AF联网进展

java - 如何通过Android应用程序获取蓝牙鼠标(连接到Android)的移动量?

android - 如何在 RecyclerView.Adapter 中使用共享首选项?

android - 来自谷歌的新底部布局

ios - 如何解决 linker command failed with exit code 1 的错误(使用 -v 查看调用)

ios - 如何在 Swift 中从 Core Data 中删除多个不同的数据?