一、AlignTextView

  1. public class AlignTextView extends AppCompatTextView {
  2. private boolean alignOnlyOneLine;
  3. public AlignTextView(Context context) {
  4. this(context, null);
  5. }
  6. public AlignTextView(Context context, @Nullable AttributeSet attrs) {
  7. this(context, attrs, 0);
  8. }
  9. public AlignTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  10. super(context, attrs, defStyleAttr);
  11. init(context, attrs);
  12. }
  13. private void init(Context context, AttributeSet attrs) {
  14. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);
  15. alignOnlyOneLine = typedArray.getBoolean(R.styleable.AlignTextView_alignOnlyOneLine, false);
  16. typedArray.recycle();
  17. setTextColor(getCurrentTextColor());
  18. }
  19. @Override
  20. public void setTextColor(int color) {
  21. super.setTextColor(color);
  22. getPaint().setColor(color);
  23. }
  24. protected void onDraw(Canvas canvas) {
  25. CharSequence content = getText();
  26. if (!(content instanceof String)) {
  27. super.onDraw(canvas);
  28. return;
  29. }
  30. String text = (String) content;
  31. Layout layout = getLayout();
  32. for (int i = 0; i < layout.getLineCount(); ++i) {
  33. int lineBaseline = layout.getLineBaseline(i) + getPaddingTop();
  34. int lineStart = layout.getLineStart(i);
  35. int lineEnd = layout.getLineEnd(i);
  36. if (alignOnlyOneLine && layout.getLineCount() == 1) {//只有一行
  37. String line = text.substring(lineStart, lineEnd);
  38. float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
  39. this.drawScaledText(canvas, line, lineBaseline, width);
  40. } else if (i == layout.getLineCount() - 1) {//最后一行
  41. canvas.drawText(text.substring(lineStart), getPaddingLeft(), lineBaseline, getPaint());
  42. break;
  43. } else {//中间行
  44. String line = text.substring(lineStart, lineEnd);
  45. float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint());
  46. this.drawScaledText(canvas, line, lineBaseline, width);
  47. }
  48. }
  49. }
  50. private void drawScaledText(Canvas canvas, String line, float baseLineY, float lineWidth) {
  51. if (line.length() < 1) {
  52. return;
  53. }
  54. float x = getPaddingLeft();
  55. boolean forceNextLine = line.charAt(line.length() - 1) == 10;
  56. int length = line.length() - 1;
  57. if (forceNextLine || length == 0) {
  58. canvas.drawText(line, x, baseLineY, getPaint());
  59. return;
  60. }
  61. float d = (getMeasuredWidth() - lineWidth - getPaddingLeft() - getPaddingRight()) / length;
  62. for (int i = 0; i < line.length(); ++i) {
  63. String c = String.valueOf(line.charAt(i));
  64. float cw = StaticLayout.getDesiredWidth(c, this.getPaint());
  65. canvas.drawText(c, x, baseLineY, this.getPaint());
  66. x += cw + d;
  67. }
  68. }
  69. }

attrs:
显示一行时使用,感觉没什么用处。

  1. <declare-styleable name="AlignTextView">
  2. <attr name="alignOnlyOneLine" format="boolean"/>
  3. </declare-styleable>

二、另外一个

https://github.com/androiddevelop/AlignTextView