1010 * author: Blankj
1111 * blog : http://blankj.com
1212 * time : 2016/08/02
13- * desc : 尺寸相关工具类
13+ * desc : utils about size
1414 * </pre>
1515 */
1616public final class SizeUtils {
@@ -20,61 +20,65 @@ private SizeUtils() {
2020 }
2121
2222 /**
23- * dp 转 px
23+ * Value of dp to value of px.
2424 *
25- * @param dpValue dp 值
26- * @return px 值
25+ * @param dpValue The value of dp.
26+ * @return value of px
2727 */
2828 public static int dp2px (final float dpValue ) {
2929 final float scale = Utils .getApp ().getResources ().getDisplayMetrics ().density ;
3030 return (int ) (dpValue * scale + 0.5f );
3131 }
3232
3333 /**
34- * px 转 dp
34+ * Value of px to value of dp.
3535 *
36- * @param pxValue px 值
37- * @return dp 值
36+ * @param pxValue The value of px.
37+ * @return value of dp
3838 */
3939 public static int px2dp (final float pxValue ) {
4040 final float scale = Utils .getApp ().getResources ().getDisplayMetrics ().density ;
4141 return (int ) (pxValue / scale + 0.5f );
4242 }
4343
4444 /**
45- * sp 转 px
45+ * Value of sp to value of px.
4646 *
47- * @param spValue sp 值
48- * @return px 值
47+ * @param spValue The value of sp.
48+ * @return value of px
4949 */
5050 public static int sp2px (final float spValue ) {
5151 final float fontScale = Utils .getApp ().getResources ().getDisplayMetrics ().scaledDensity ;
5252 return (int ) (spValue * fontScale + 0.5f );
5353 }
5454
5555 /**
56- * px 转 sp
56+ * Value of px to value of sp.
5757 *
58- * @param pxValue px 值
59- * @return sp 值
58+ * @param pxValue The value of px.
59+ * @return value of sp
6060 */
6161 public static int px2sp (final float pxValue ) {
6262 final float fontScale = Utils .getApp ().getResources ().getDisplayMetrics ().scaledDensity ;
6363 return (int ) (pxValue / fontScale + 0.5f );
6464 }
6565
6666 /**
67- * 各种单位转换
68- * <p>该方法存在于 TypedValue</p>
67+ * Converts an unpacked complex data value holding a dimension to its final floating
68+ * point value. The two parameters <var>unit</var> and <var>value</var>
69+ * are as in {@link TypedValue#TYPE_DIMENSION}.
6970 *
70- * @param unit 单位
71- * @param value 值
72- * @param metrics DisplayMetrics
73- * @return 转换结果
71+ * @param unit The unit to convert from.
72+ * @param value The value to apply the unit to.
73+ * @param metrics Current display metrics to use in the conversion --
74+ * supplies display density and scaling information.
75+ * @return The complex floating point value multiplied by the appropriate
76+ * metrics depending on its unit.
7477 */
7578 public static float applyDimension (final int unit ,
7679 final float value ,
7780 final DisplayMetrics metrics ) {
81+
7882 switch (unit ) {
7983 case TypedValue .COMPLEX_UNIT_PX :
8084 return value ;
@@ -93,9 +97,8 @@ public static float applyDimension(final int unit,
9397 }
9498
9599 /**
96- * 在 onCreate 中获取视图的尺寸
97- * <p>需回调 onGetSizeListener 接口,在 onGetSize 中获取 view 宽高</p>
98- * <p>用法示例如下所示</p>
100+ * Force get the size of view.
101+ * <p>e.g.</p>
99102 * <pre>
100103 * SizeUtils.forceGetViewSize(view, new SizeUtils.onGetSizeListener() {
101104 * Override
@@ -105,8 +108,8 @@ public static float applyDimension(final int unit,
105108 * });
106109 * </pre>
107110 *
108- * @param view 视图
109- * @param listener 监听器
111+ * @param view The view.
112+ * @param listener The get size listener.
110113 */
111114 public static void forceGetViewSize (final View view , final onGetSizeListener listener ) {
112115 view .post (new Runnable () {
@@ -120,17 +123,30 @@ public void run() {
120123 }
121124
122125 /**
123- * 获取到 View 尺寸的监听
126+ * Return the width of view.
127+ *
128+ * @param view The view.
129+ * @return the width of view
124130 */
125- public interface onGetSizeListener {
126- void onGetSize (View view );
131+ public static int getMeasuredWidth (final View view ) {
132+ return measureView (view )[0 ];
133+ }
134+
135+ /**
136+ * Return the height of view.
137+ *
138+ * @param view The view.
139+ * @return the height of view
140+ */
141+ public static int getMeasuredHeight (final View view ) {
142+ return measureView (view )[1 ];
127143 }
128144
129145 /**
130- * 测量视图尺寸
146+ * Measure the view.
131147 *
132- * @param view 视图
133- * @return arr[0]: 视图宽度 , arr[1]: 视图高度
148+ * @param view The view.
149+ * @return arr[0]: view's width , arr[1]: view's height
134150 */
135151 public static int [] measureView (final View view ) {
136152 ViewGroup .LayoutParams lp = view .getLayoutParams ();
@@ -152,23 +168,8 @@ public static int[] measureView(final View view) {
152168 return new int []{view .getMeasuredWidth (), view .getMeasuredHeight ()};
153169 }
154170
155- /**
156- * 获取测量视图宽度
157- *
158- * @param view 视图
159- * @return 视图宽度
160- */
161- public static int getMeasuredWidth (final View view ) {
162- return measureView (view )[0 ];
163- }
164171
165- /**
166- * 获取测量视图高度
167- *
168- * @param view 视图
169- * @return 视图高度
170- */
171- public static int getMeasuredHeight (final View view ) {
172- return measureView (view )[1 ];
172+ public interface onGetSizeListener {
173+ void onGetSize (View view );
173174 }
174175}
0 commit comments