Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xiaoxuan_i809
AndroidUtilCode
提交
2679130d
A
AndroidUtilCode
项目概览
xiaoxuan_i809
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2679130d
编写于
3月 18, 2019
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 03/18 log
上级
18395d74
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
37 deletion
+44
-37
CHANGELOG.md
CHANGELOG.md
+2
-0
utilcode/lib/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java
.../main/java/com/blankj/utilcode/util/AdaptScreenUtils.java
+42
-37
未找到文件。
CHANGELOG.md
浏览文件 @
2679130d
*
`19/03/17`
[fix] The ugly UI.
*
`19/03/14`
[fix] AdaptScreenUtils didn't work on some HaWei tablet.
*
`19/03/08`
[add] LogUtils support multi process. Publish v1.23.7.
*
`19/03/02`
[fix] LogUtils#file.
*
`19/02/28`
[fix] ImageUtils#calculateInSampleSize. Publish v1.23.6.
...
...
utilcode/lib/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java
浏览文件 @
2679130d
...
...
@@ -5,30 +5,29 @@ import android.util.DisplayMetrics;
import
android.util.Log
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.List
;
public
final
class
AdaptScreenUtils
{
private
static
boolean
sIsInit
;
private
static
Field
sMetricsField
;
private
static
List
<
Field
>
sMetricsFields
;
/**
* Adapt for the horizontal screen, and call it in [android.app.Activity.getResources].
*/
public
static
Resources
adaptWidth
(
Resources
resources
,
int
designWidth
)
{
DisplayMetrics
dm
=
getDisplayMetrics
(
resources
);
float
newXdpi
=
dm
.
xdpi
=
(
dm
.
widthPixels
*
72
f
)
/
designWidth
;
setAppDmXdpi
(
newXdpi
);
public
static
Resources
adaptWidth
(
final
Resources
resources
,
final
int
designWidth
)
{
float
newXdpi
=
(
Resources
.
getSystem
().
getDisplayMetrics
().
widthPixels
*
72
f
)
/
designWidth
;
applyDisplayMetrics
(
resources
,
newXdpi
);
return
resources
;
}
/**
* Adapt for the vertical screen, and call it in [android.app.Activity.getResources].
*/
public
static
Resources
adaptHeight
(
Resources
resources
,
int
designHeight
)
{
DisplayMetrics
dm
=
getDisplayMetrics
(
resources
);
float
newXdpi
=
dm
.
xdpi
=
(
dm
.
heightPixels
*
72
f
)
/
designHeight
;
setAppDmXdpi
(
newXdpi
);
public
static
Resources
adaptHeight
(
final
Resources
resources
,
final
int
designHeight
)
{
float
newXdpi
=
(
Resources
.
getSystem
().
getDisplayMetrics
().
heightPixels
*
72
f
)
/
designHeight
;
applyDisplayMetrics
(
resources
,
newXdpi
);
return
resources
;
}
...
...
@@ -36,10 +35,9 @@ public final class AdaptScreenUtils {
* @param resources The resources.
* @return the resource
*/
public
static
Resources
closeAdapt
(
Resources
resources
)
{
DisplayMetrics
dm
=
getDisplayMetrics
(
resources
);
float
newXdpi
=
dm
.
xdpi
=
dm
.
density
*
72
;
setAppDmXdpi
(
newXdpi
);
public
static
Resources
closeAdapt
(
final
Resources
resources
)
{
float
newXdpi
=
Resources
.
getSystem
().
getDisplayMetrics
().
density
*
72
f
;
applyDisplayMetrics
(
resources
,
newXdpi
);
return
resources
;
}
...
...
@@ -49,7 +47,7 @@ public final class AdaptScreenUtils {
* @param ptValue The value of pt.
* @return value of px
*/
public
static
int
pt2Px
(
float
ptValue
)
{
public
static
int
pt2Px
(
f
inal
f
loat
ptValue
)
{
DisplayMetrics
metrics
=
Utils
.
getApp
().
getResources
().
getDisplayMetrics
();
return
(
int
)
(
ptValue
*
metrics
.
xdpi
/
72
f
+
0.5
);
}
...
...
@@ -60,33 +58,31 @@ public final class AdaptScreenUtils {
* @param pxValue The value of px.
* @return value of pt
*/
public
static
int
px2Pt
(
float
pxValue
)
{
public
static
int
px2Pt
(
f
inal
f
loat
pxValue
)
{
DisplayMetrics
metrics
=
Utils
.
getApp
().
getResources
().
getDisplayMetrics
();
return
(
int
)
(
pxValue
*
72
/
metrics
.
xdpi
+
0.5
);
}
private
static
void
setAppDmXdpi
(
final
float
xdpi
)
{
Utils
.
getApp
().
getResources
().
getDisplayMetrics
().
xdpi
=
xdpi
;
private
static
void
applyDisplayMetrics
(
final
Resources
resources
,
final
float
newXdpi
)
{
resources
.
getDisplayMetrics
().
xdpi
=
newXdpi
;
Utils
.
getApp
().
getResources
().
getDisplayMetrics
().
xdpi
=
newXdpi
;
applyOtherDisplayMetrics
(
resources
,
newXdpi
);
}
private
static
DisplayMetrics
getDisplayMetrics
(
Resources
resources
)
{
DisplayMetrics
realMetrics
=
getRealMetrics
(
resources
);
if
(
realMetrics
!=
null
)
return
realMetrics
;
return
resources
.
getDisplayMetrics
();
}
private
static
DisplayMetrics
getRealMetrics
(
final
Resources
resources
)
{
if
(!
sIsInit
)
{
DisplayMetrics
ret
=
null
;
private
static
void
applyOtherDisplayMetrics
(
final
Resources
resources
,
final
float
newXdpi
)
{
if
(
sMetricsFields
==
null
)
{
sMetricsFields
=
new
ArrayList
<>();
Class
resCls
=
resources
.
getClass
();
Field
[]
declaredFields
=
resCls
.
getDeclaredFields
();
while
(
ret
==
null
&&
declaredFields
!=
null
&&
declaredFields
.
length
>
0
)
{
while
(
declaredFields
!=
null
&&
declaredFields
.
length
>
0
)
{
for
(
Field
field
:
declaredFields
)
{
if
(
field
.
getType
().
isAssignableFrom
(
DisplayMetrics
.
class
))
{
field
.
setAccessible
(
true
);
sMetricsField
=
field
;
ret
=
getMetricsFromField
(
resources
);
if
(
ret
!=
null
)
break
;
DisplayMetrics
tmpDm
=
getMetricsFromField
(
resources
,
field
);
if
(
tmpDm
!=
null
)
{
sMetricsFields
.
add
(
field
);
tmpDm
.
xdpi
=
newXdpi
;
}
}
}
resCls
=
resCls
.
getSuperclass
();
...
...
@@ -96,16 +92,25 @@ public final class AdaptScreenUtils {
break
;
}
}
sIsInit
=
true
;
return
ret
;
}
else
{
applyMetricsFields
(
resources
,
newXdpi
);
}
}
private
static
void
applyMetricsFields
(
final
Resources
resources
,
final
float
newXdpi
)
{
for
(
Field
metricsField
:
sMetricsFields
)
{
try
{
DisplayMetrics
dm
=
(
DisplayMetrics
)
metricsField
.
get
(
resources
);
if
(
dm
!=
null
)
dm
.
xdpi
=
newXdpi
;
}
catch
(
Exception
e
)
{
Log
.
e
(
"AdaptScreenUtils"
,
"applyMetricsFields: "
+
e
);
}
}
if
(
sMetricsField
==
null
)
return
null
;
return
getMetricsFromField
(
resources
);
}
private
static
DisplayMetrics
getMetricsFromField
(
final
Resources
resources
)
{
private
static
DisplayMetrics
getMetricsFromField
(
final
Resources
resources
,
final
Field
field
)
{
try
{
return
(
DisplayMetrics
)
sMetricsF
ield
.
get
(
resources
);
return
(
DisplayMetrics
)
f
ield
.
get
(
resources
);
}
catch
(
Exception
e
)
{
Log
.
e
(
"AdaptScreenUtils"
,
"getMetricsFromField: "
+
e
);
return
null
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录