Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package com.example.niuky.design.widget;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.os.Build;
  9. import android.text.Spannable;
  10. import android.text.SpannableString;
  11. import android.text.style.ForegroundColorSpan;
  12. import android.util.AttributeSet;
  13. import android.widget.TextView;
  14.  
  15. import com.example.niuky.design.R;
  16.  
  17. /**
  18. * Created by NiuKY on 7/10.
  19. */
  20. public class ProgressTextView extends TextView {
  21.  
  22. private int progress;
  23. private int max = 100;
  24. private int progressColor;
  25. private ForegroundColorSpan foregroundColorSpan;
  26. private Spannable spannableString = new SpannableString(getText());
  27. private boolean isChangingProgress;
  28.  
  29.  
  30. public ProgressTextView(Context context) {
  31. super(context);
  32. init();
  33. }
  34.  
  35. public ProgressTextView(Context context, AttributeSet attrs) {
  36. super(context, attrs);
  37. init();
  38. getAttributes(context, attrs);
  39. }
  40.  
  41.  
  42. public ProgressTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  43. super(context, attrs, defStyleAttr);
  44. init();
  45. getAttributes(context, attrs);
  46. }
  47.  
  48. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  49. public ProgressTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  50. super(context, attrs, defStyleAttr, defStyleRes);
  51. init();
  52. getAttributes(context, attrs);
  53. }
  54.  
  55. private void init() {
  56. foregroundColorSpan = new ForegroundColorSpan(Color.WHITE);
  57. }
  58.  
  59. private void getAttributes(Context context, AttributeSet attrs) {
  60. final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressTextView);
  61. try {
  62. if (a != null) {
  63. setProgressColor(a.getColor(R.styleable.ProgressTextView_ptv_progressColor, 0xff00ff00));
  64. setProgress(a.getInt(R.styleable.ProgressTextView_ptv_progress, 0));
  65. setMax(a.getInt(R.styleable.ProgressTextView_ptv_max, 100));
  66. }
  67. } finally {
  68. if (a != null) {
  69. a.recycle();
  70. }
  71. }
  72. }
  73.  
  74.  
  75. public synchronized void setProgress(final int progress) {
  76. this.progress = progress;
  77. isChangingProgress = true;
  78. invalidate();
  79. }
  80.  
  81. public synchronized int getProgress() {
  82. return this.progress;
  83. }
  84.  
  85.  
  86. public synchronized void setMax(final int max) {
  87. this.max = max;
  88. isChangingProgress = true;
  89. invalidate();
  90. }
  91.  
  92. public synchronized int getMax() {
  93. return this.max;
  94. }
  95.  
  96. public void setProgressColor(final int progressColor) {
  97. this.progressColor = progressColor;
  98. foregroundColorSpan = new ForegroundColorSpan(progressColor);
  99. isChangingProgress = true;
  100. invalidate();
  101. }
  102.  
  103. public int getProgressColor() {
  104. return this.progressColor;
  105. }
  106.  
  107.  
  108. @Override
  109. protected void onDraw(Canvas canvas) {
  110. super.onDraw(canvas);
  111. if (isChangingProgress) {
  112. final float procentProgress = (float) progress / max;
  113. spannableString.setSpan(foregroundColorSpan, 0, (int) (procentProgress * getText().length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  114. setText(spannableString);
  115. isChangingProgress = false;
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement