Android屏幕信息工具类

244 阅读2分钟

用于返回屏幕大小相关信息,方便你根据此来设计控件尺寸

快速开始

点击ScreenSizeUtils查看源码,欢迎Star,fork,并在issue提出改进意见

implementation 'io.github.sakurajimamaii:VastTools:0.0.3'

概括

私有属性

Param NameParam Class
sRealSizesArray<Point?>
DefaultDeviceAspectRatioDevice
默认Device,将屏幕长宽比大于1.97的设备定义为全面屏
defaultAspectRatioListMutableList
默认设备屏幕长宽比信息类列表
AspectRatioDeviceListMutableList
设备屏幕长宽比信息类列表

私有方法

ReturnsMethod
Intfun getScreenHeight(context: Context)
Intfun getScreenRealHeightApi30Above(context: Context)
Api 30 以上推荐使用
Intfun getScreenRealHeightApi30Down(context: Context)
Api 30 以下推荐使用

公有方法

ReturnsMethod
Booleanfun isAllScreenDeviceApi31(context: Context,deviceTag: String = DefaultDevice.tag)
Api 31 推荐使用
Booleanfun isAllScreenDeviceApi30(context: Context,deviceTag: String = DefaultDevice.tag)
Api 30 推荐使用
Booleanfun isAllScreenDeviceApi30Down(context: Context,deviceTag: String = DefaultDevice.tag)
Api 30 以下推荐使用
Intfun getMobileScreenWidth(context: Context)
Intfun getMobileScreenHeightApi31(context: Context)
Api 31 推荐使用
Intfun getMobileScreenHeightApi30(context: Context)
Api 30 推荐使用
Intfun getMobileScreenHeightApi30Down(context: Context)
Api 30 以下推荐使用
Unitfun addDevice(vararg devices: AspectRatioDevice)
添加设备屏幕的长宽比信息。
Unitfun removeDevice(device: AspectRatioDevice)
删除设备屏幕的长宽比信息。
Unitfun resetDeviceList()
重置AspectRatioDeviceList

公有方法

全面屏判断

// Use in api 31
fun isAllScreenDeviceApi31(context: Context,deviceTag: String = DefaultDevice.tag): Boolean
// Use in api 30
fun isAllScreenDeviceApi30(context: Context,deviceTag: String = DefaultDevice.tag): Boolean
// Use in api 30 down
fun isAllScreenDeviceApi30Down(context: Context,deviceTag: String = DefaultDevice.tag): Boolean

判断你的设备是否是全面屏

参数

context:Context 函数所在的上下文 deviceTag:String 你设备的 tag 借此判断你设备全面屏应该符合的屏幕长宽比

返回值

Boolean 如果是全面屏返回 true ,否则返回 false

getMobileScreenWidth

fun getMobileScreenWidth(context: Context):Int

返回设备的屏幕宽度

参数

context:Context 函数所在的上下文

返回值

Int 返回屏幕的宽度,单位是 pixels ,返回 0 代表获取失败

屏幕高度获取

// Use in api 31
fun getMobileScreenHeightApi31(context: Context):Int
// Use in api 30
fun getMobileScreenHeightApi30(context: Context):Int
// Use in api 30 down
fun getMobileScreenHeightApi30Down(context: Context):Int

返回设备的屏幕高度

参数

context:Context 函数所在的上下文

返回值

Int 返回屏幕的高度,单位是 pixels ,返回 0 代表获取失败