public class SizeUtils
extends java.lang.Object
author: Blankj blog : http://blankj.com time : 2016/8/2 desc : 尺寸相关工具类
Modifier and Type | Class and Description |
---|---|
static interface |
SizeUtils.onGetSizeListener
获取到View尺寸的监听
|
Modifier and Type | Method and Description |
---|---|
static float |
applyDimension(int unit,
float value,
DisplayMetrics metrics)
各种单位转换
|
static int |
dp2px(float dpValue)
dp转px
|
static void |
forceGetViewSize(View view,
SizeUtils.onGetSizeListener listener)
在onCreate中获取视图的尺寸
|
static int |
getMeasuredHeight(View view)
获取测量视图高度
|
static int |
getMeasuredWidth(View view)
获取测量视图宽度
|
static int[] |
measureView(View view)
测量视图尺寸
|
static int |
px2dp(float pxValue)
px转dp
|
static int |
px2sp(float pxValue)
px转sp
|
static int |
sp2px(float spValue)
sp转px
|
public static int dp2px(float dpValue)
dpValue
- dp值public static int px2dp(float pxValue)
pxValue
- px值public static int sp2px(float spValue)
spValue
- sp值public static int px2sp(float pxValue)
pxValue
- px值public static float applyDimension(int unit, float value, DisplayMetrics metrics)
该方法存在于TypedValue
unit
- 单位value
- 值metrics
- DisplayMetricspublic static void forceGetViewSize(View view, SizeUtils.onGetSizeListener listener)
需回调onGetSizeListener接口,在onGetSize中获取view宽高
用法示例如下所示
SizeUtils.forceGetViewSize(view, new SizeUtils.onGetSizeListener() { Override public void onGetSize(View view) { view.getWidth(); } });
view
- 视图listener
- 监听器public static int[] measureView(View view)
view
- 视图public static int getMeasuredWidth(View view)
view
- 视图public static int getMeasuredHeight(View view)
view
- 视图