Guest User

Untitled

a guest
Nov 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import android.graphics.Paint;
  2. import android.widget.TextView;
  3.  
  4. import java.util.Arrays;
  5.  
  6. /**
  7. * Helpers for dealing with custom typefaces and measuring text to display
  8. */
  9. public class TypefaceUtils {
  10.  
  11. /**
  12. * Find the maximum number of digits in the given numbers
  13. *
  14. * @param numbers
  15. * @return max digits
  16. */
  17. public static int getMaxDigits(int... numbers) {
  18. int max = 1;
  19. for (int number : numbers) {
  20. max = Math.max(max, (int) Math.log10(number) + 1);
  21. }
  22. return max;
  23. }
  24.  
  25. /**
  26. * Get width of number of digits
  27. *
  28. * @param view
  29. * @param numberOfDigits
  30. * @return number width
  31. */
  32. public static int getWidth(TextView view, int numberOfDigits) {
  33. Paint paint = new Paint();
  34. paint.setTypeface(view.getTypeface());
  35. paint.setTextSize(view.getTextSize());
  36. char[] text = new char[numberOfDigits];
  37. Arrays.fill(text, '0');
  38. return Math.round(paint.measureText(text, 0, text.length));
  39. }
  40. }
Add Comment
Please, Sign In to add comment