11package com .blankj .utilcode .utils ;
22
3+ import android .content .Context ;
34import android .content .res .Resources ;
45import android .graphics .Bitmap ;
56import android .graphics .BitmapFactory ;
@@ -49,7 +50,7 @@ public static String bytes2HexString(byte[] bytes) {
4950 * hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
5051 *
5152 * @param hexString 十六进制字符串
52- * @return byte数组
53+ * @return 字节数组
5354 */
5455 public static byte [] hexString2Bytes (String hexString ) {
5556 int len = hexString .length ();
@@ -83,8 +84,8 @@ private static int hex2Dec(char hexChar) {
8384 /**
8485 * charArr转byteArr
8586 *
86- * @param chars 待转的char数组
87- * @return byte数组
87+ * @param chars 字符数组
88+ * @return 字节数组
8889 */
8990 public static byte [] chars2Bytes (char [] chars ) {
9091 int len = chars .length ;
@@ -98,8 +99,8 @@ public static byte[] chars2Bytes(char[] chars) {
9899 /**
99100 * byteArr转charArr
100101 *
101- * @param bytes 待转的byte数组
102- * @return char数组
102+ * @param bytes 字节数组
103+ * @return 字符数组
103104 */
104105 public static char [] bytes2Chars (byte [] bytes ) {
105106 int len = bytes .length ;
@@ -196,7 +197,7 @@ public static InputStream string2InputStream(String string, String charsetName)
196197 * bitmap转byteArr
197198 *
198199 * @param bitmap bitmap对象
199- * @param format 图片格式
200+ * @param format 格式
200201 * @return 字节数组
201202 */
202203 public static byte [] bitmap2Bytes (Bitmap bitmap , Bitmap .CompressFormat format ) {
@@ -236,4 +237,74 @@ public static Bitmap drawable2Bitmap(Drawable drawable) {
236237 public static Drawable bitmap2Drawable (Resources resources , Bitmap bitmap ) {
237238 return bitmap == null ? null : new BitmapDrawable (resources , bitmap );
238239 }
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+ }
239310}
0 commit comments