1
1
package com .blankj .utilcode .utils ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .res .Resources ;
4
5
import android .graphics .Bitmap ;
5
6
import android .graphics .BitmapFactory ;
@@ -49,7 +50,7 @@ public static String bytes2HexString(byte[] bytes) {
49
50
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
50
51
*
51
52
* @param hexString 十六进制字符串
52
- * @return byte数组
53
+ * @return 字节数组
53
54
*/
54
55
public static byte [] hexString2Bytes (String hexString ) {
55
56
int len = hexString .length ();
@@ -83,8 +84,8 @@ private static int hex2Dec(char hexChar) {
83
84
/**
84
85
* charArr转byteArr
85
86
*
86
- * @param chars 待转的char数组
87
- * @return byte数组
87
+ * @param chars 字符数组
88
+ * @return 字节数组
88
89
*/
89
90
public static byte [] chars2Bytes (char [] chars ) {
90
91
int len = chars .length ;
@@ -98,8 +99,8 @@ public static byte[] chars2Bytes(char[] chars) {
98
99
/**
99
100
* byteArr转charArr
100
101
*
101
- * @param bytes 待转的byte数组
102
- * @return char数组
102
+ * @param bytes 字节数组
103
+ * @return 字符数组
103
104
*/
104
105
public static char [] bytes2Chars (byte [] bytes ) {
105
106
int len = bytes .length ;
@@ -196,7 +197,7 @@ public static InputStream string2InputStream(String string, String charsetName)
196
197
* bitmap转byteArr
197
198
*
198
199
* @param bitmap bitmap对象
199
- * @param format 图片格式
200
+ * @param format 格式
200
201
* @return 字节数组
201
202
*/
202
203
public static byte [] bitmap2Bytes (Bitmap bitmap , Bitmap .CompressFormat format ) {
@@ -236,4 +237,74 @@ public static Bitmap drawable2Bitmap(Drawable drawable) {
236
237
public static Drawable bitmap2Drawable (Resources resources , Bitmap bitmap ) {
237
238
return bitmap == null ? null : new BitmapDrawable (resources , bitmap );
238
239
}
240
+
241
+ /**
242
+ * drawable转byteArr
243
+ *
244
+ * @param drawable drawable对象
245
+ * @param format 格式
246
+ * @return 字节数组
247
+ */
248
+ public static byte [] drawable2Bytes (Drawable drawable , Bitmap .CompressFormat format ) {
249
+ return bitmap2Bytes (drawable2Bitmap (drawable ), format );
250
+ }
251
+
252
+ /**
253
+ * byteArr转drawable
254
+ *
255
+ * @param resources resources对象
256
+ * @param bytes 字节数组
257
+ * @return drawable对象
258
+ */
259
+ public static Drawable bytes2Drawable (Resources resources , byte [] bytes ) {
260
+ return bitmap2Drawable (resources , bytes2Bitmap (bytes ));
261
+ }
262
+
263
+ /**
264
+ * dp转px
265
+ *
266
+ * @param context 上下文
267
+ * @param dpValue dp值
268
+ * @return px值
269
+ */
270
+ public static int dp2px (Context context , float dpValue ) {
271
+ final float scale = context .getResources ().getDisplayMetrics ().density ;
272
+ return (int ) (dpValue * scale + 0.5f );
273
+ }
274
+
275
+ /**
276
+ * px转dp
277
+ *
278
+ * @param context 上下文
279
+ * @param pxValue px值
280
+ * @return dp值
281
+ */
282
+ public static int px2dp (Context context , float pxValue ) {
283
+ final float scale = context .getResources ().getDisplayMetrics ().density ;
284
+ return (int ) (pxValue / scale + 0.5f );
285
+ }
286
+
287
+ /**
288
+ * sp转px
289
+ *
290
+ * @param context 上下文
291
+ * @param spValue sp值
292
+ * @return px值
293
+ */
294
+ public static int sp2px (Context context , float spValue ) {
295
+ final float fontScale = context .getResources ().getDisplayMetrics ().scaledDensity ;
296
+ return (int ) (spValue * fontScale + 0.5f );
297
+ }
298
+
299
+ /**
300
+ * px转sp
301
+ *
302
+ * @param context 上下文
303
+ * @param pxValue px值
304
+ * @return sp值
305
+ */
306
+ public static int px2sp (Context context , float pxValue ) {
307
+ final float fontScale = context .getResources ().getDisplayMetrics ().scaledDensity ;
308
+ return (int ) (pxValue / fontScale + 0.5f );
309
+ }
239
310
}
0 commit comments