Utils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.jaygoo.widget;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Matrix;
  7. import android.graphics.NinePatch;
  8. import android.graphics.Paint;
  9. import android.graphics.Rect;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.graphics.drawable.Drawable;
  12. import android.os.Build;
  13. import androidx.annotation.ColorRes;
  14. import androidx.core.content.ContextCompat;
  15. import android.util.Log;
  16. /**
  17. * ================================================
  18. * 作 者:JayGoo
  19. * 版 本:
  20. * 创建日期:2018/5/8
  21. * 描 述:
  22. * ================================================
  23. */
  24. public class Utils {
  25. private static final String TAG = "RangeSeekBar";
  26. public static void print(String log) {
  27. Log.d(TAG, log);
  28. }
  29. public static void print(Object... logs) {
  30. StringBuilder stringBuilder = new StringBuilder();
  31. for (Object log : logs) {
  32. stringBuilder.append(log);
  33. }
  34. Log.d(TAG, stringBuilder.toString());
  35. }
  36. public static Bitmap drawableToBitmap(Context context, int width, int height, int drawableId) {
  37. if (context == null || width <= 0 || height <= 0 || drawableId == 0) return null;
  38. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  39. return Utils.drawableToBitmap(width, height, context.getResources().getDrawable(drawableId, null));
  40. } else {
  41. return Utils.drawableToBitmap(width, height, context.getResources().getDrawable(drawableId));
  42. }
  43. }
  44. /**
  45. * make a drawable to a bitmap
  46. *
  47. * @param drawable drawable you want convert
  48. * @return converted bitmap
  49. */
  50. public static Bitmap drawableToBitmap(int width, int height, Drawable drawable) {
  51. Bitmap bitmap = null;
  52. try {
  53. if (drawable instanceof BitmapDrawable) {
  54. BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
  55. bitmap = bitmapDrawable.getBitmap();
  56. if (bitmap != null && bitmap.getHeight() > 0) {
  57. Matrix matrix = new Matrix();
  58. float scaleWidth = width * 1.0f / bitmap.getWidth();
  59. float scaleHeight = height * 1.0f / bitmap.getHeight();
  60. matrix.postScale(scaleWidth, scaleHeight);
  61. bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  62. return bitmap;
  63. }
  64. }
  65. bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  66. Canvas canvas = new Canvas(bitmap);
  67. drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  68. drawable.draw(canvas);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return bitmap;
  73. }
  74. /**
  75. * draw 9Path
  76. *
  77. * @param canvas Canvas
  78. * @param bmp 9path bitmap
  79. * @param rect 9path rect
  80. */
  81. public static void drawNinePath(Canvas canvas, Bitmap bmp, Rect rect) {
  82. NinePatch.isNinePatchChunk(bmp.getNinePatchChunk());
  83. NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null);
  84. patch.draw(canvas, rect);
  85. }
  86. public static void drawBitmap(Canvas canvas, Paint paint, Bitmap bmp, Rect rect) {
  87. try {
  88. if (NinePatch.isNinePatchChunk(bmp.getNinePatchChunk())) {
  89. drawNinePath(canvas, bmp, rect);
  90. return;
  91. }
  92. } catch (Exception e) {
  93. }
  94. canvas.drawBitmap(bmp, rect.left, rect.top, paint);
  95. }
  96. public static int dp2px(Context context, float dpValue) {
  97. if (context == null || compareFloat(0f, dpValue) == 0) return 0;
  98. final float scale = context.getResources().getDisplayMetrics().density;
  99. return (int) (dpValue * scale + 0.5f);
  100. }
  101. /**
  102. * Compare the size of two floating point numbers
  103. *
  104. * @param a
  105. * @param b
  106. * @return 1 is a > b
  107. * -1 is a < b
  108. * 0 is a == b
  109. */
  110. public static int compareFloat(float a, float b) {
  111. int ta = Math.round(a * 1000000);
  112. int tb = Math.round(b * 1000000);
  113. if (ta > tb) {
  114. return 1;
  115. } else if (ta < tb) {
  116. return -1;
  117. } else {
  118. return 0;
  119. }
  120. }
  121. /**
  122. * Compare the size of two floating point numbers with accuracy
  123. *
  124. * @param a
  125. * @param b
  126. * @return 1 is a > b
  127. * -1 is a < b
  128. * 0 is a == b
  129. */
  130. public static int compareFloat(float a, float b, int degree) {
  131. if (Math.abs(a - b) < Math.pow(0.1, degree)) {
  132. return 0;
  133. } else {
  134. if (a < b) {
  135. return -1;
  136. } else {
  137. return 1;
  138. }
  139. }
  140. }
  141. public static float parseFloat(String s) {
  142. try {
  143. return Float.parseFloat(s);
  144. } catch (NumberFormatException e) {
  145. return 0f;
  146. }
  147. }
  148. public static Rect measureText(String text, float textSize) {
  149. Paint paint = new Paint();
  150. Rect textRect = new Rect();
  151. paint.setTextSize(textSize);
  152. paint.getTextBounds(text, 0, text.length(), textRect);
  153. paint.reset();
  154. return textRect;
  155. }
  156. public static boolean verifyBitmap(Bitmap bitmap) {
  157. if (bitmap == null || bitmap.isRecycled() || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. public static int getColor(Context context, @ColorRes int colorId) {
  163. if (context != null) {
  164. return ContextCompat.getColor(context.getApplicationContext(), colorId);
  165. }
  166. return Color.WHITE;
  167. }
  168. }