Tatarize

Radial Progress Bar

Sep 17th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. package photoembroidery.tat.radialprogressbar;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.ValueAnimator;
  5. import android.content.Context;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.graphics.SweepGradient;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. public class RadialProgressBar extends View {
  14. static final int COLOR_BACKGROUNDPURPLE = Color.argb(0xFF, 32, 24, 79);
  15. static final int COLOR_LIGHTPURPLE = Color.argb(0xFF, 98, 66, 141);
  16. static final int TICKS = 200;
  17.  
  18. float progress = 0;
  19. Paint linePaint;
  20. Paint textPaint;
  21. Paint circlePaint;
  22. boolean dirty = true;
  23. float[] lines;
  24. int drawlines = TICKS;
  25. float textX, textY;
  26. float cx, cy, cr;
  27.  
  28. public RadialProgressBar(Context context) {
  29. super(context);
  30. }
  31.  
  32. public RadialProgressBar(Context context, AttributeSet attrs) {
  33. super(context, attrs);
  34. }
  35.  
  36. public RadialProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
  37. super(context, attrs, defStyleAttr);
  38. }
  39.  
  40. public void update() {
  41. float width = getWidth();
  42. float height = getHeight();
  43. float dim = Math.min(width, height);
  44. drawlines = (int) (TICKS * progress * 4);
  45. SweepGradient sweep = new SweepGradient(dim / 2, height / 2, new int[]{COLOR_LIGHTPURPLE, Color.WHITE}, new float[]{0, progress});
  46. linePaint.setShader(sweep);
  47. double a = (Math.PI * 2) * progress;
  48. float circlePositionRadius = (dim * .95f) / 2;
  49. cx = (float) ((dim / 2) + Math.cos(a) * circlePositionRadius);
  50. cy = (float) ((height / 2) + Math.sin(a) * circlePositionRadius);
  51. textX = ((dim - textPaint.measureText(getProgressText())) / 2);
  52. textY = ((height + textPaint.measureText("a")) / 2);
  53. }
  54.  
  55. public void init() {
  56. if (textPaint == null) textPaint = new Paint();
  57. if (linePaint == null) linePaint = new Paint();
  58. if (circlePaint == null) circlePaint = new Paint();
  59.  
  60. float width = getWidth();
  61. float height = getHeight();
  62. float dim = Math.min(width, height);
  63. textPaint.setTextSize((int) (dim / 7));
  64. textPaint.setColor(COLOR_LIGHTPURPLE);
  65.  
  66. linePaint.setStyle(Paint.Style.STROKE);
  67. linePaint.setStrokeWidth(10);
  68.  
  69. circlePaint.setStyle(Paint.Style.STROKE);
  70. circlePaint.setStrokeWidth(10);
  71. circlePaint.setColor(Color.WHITE);
  72.  
  73. if (lines == null) {
  74. lines = new float[4 * TICKS];
  75. }
  76.  
  77. double a = 0;
  78. double step = ((2 * Math.PI) / TICKS);
  79. float r0 = (dim * .9f) / 2;
  80. float r1 = (dim / 2);
  81. for (int i = 0, s = lines.length; i < s; i += 4) {
  82. a += step;
  83. lines[i] = (float) ((dim / 2) + Math.cos(a) * r0);
  84. lines[i + 1] = (float) ((height / 2) + Math.sin(a) * r0);
  85. lines[i + 2] = (float) ((dim / 2) + Math.cos(a) * r1);
  86. lines[i + 3] = (float) ((height / 2) + Math.sin(a) * r1);
  87. }
  88. cr = (dim * 0.1f) / 2;
  89. dirty = false;
  90. update();
  91. }
  92.  
  93. private String getProgressText() {
  94. return String.format("%.2f%%", 100 * progress);
  95. }
  96.  
  97. @Override
  98. protected void onDraw(Canvas canvas) {
  99. if (dirty) init();
  100. canvas.drawColor(COLOR_BACKGROUNDPURPLE);
  101. canvas.drawLines(lines, 0, drawlines, linePaint);
  102. canvas.drawText(getProgressText(), textX, textY, textPaint);
  103. canvas.drawCircle(cx, cy, cr, circlePaint);
  104. }
  105.  
  106. @Override
  107. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  108. int desiredWidth = getResources().getDimensionPixelSize(R.dimen.preferSize);
  109. int desiredHeight = desiredWidth;
  110.  
  111. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  112. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  113. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  114. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  115.  
  116. int width;
  117. int height;
  118.  
  119. //Measure Width
  120. if (widthMode == MeasureSpec.EXACTLY) {
  121. //Must be this size
  122. width = widthSize;
  123. } else if (widthMode == MeasureSpec.AT_MOST) {
  124. //Can't be bigger than...
  125. width = Math.min(desiredWidth, widthSize);
  126. } else {
  127. //Be whatever you want
  128. width = desiredWidth;
  129. }
  130.  
  131. //Measure Height
  132. if (heightMode == MeasureSpec.EXACTLY) {
  133. //Must be this size
  134. height = heightSize;
  135. } else if (heightMode == MeasureSpec.AT_MOST) {
  136. //Can't be bigger than...
  137. height = Math.min(desiredHeight, heightSize);
  138. } else {
  139. //Be whatever you want
  140. height = desiredHeight;
  141. }
  142.  
  143. //MUST CALL THIS
  144. setMeasuredDimension(width, height);
  145. }
  146.  
  147. public float getProgress() {
  148. return progress;
  149. }
  150.  
  151. public void setProgress(float progress) {
  152. this.progress = progress;
  153. dirty = true;
  154. }
  155.  
  156.  
  157. ValueAnimator animator;
  158.  
  159. public void animate_progress() {
  160. animator = ValueAnimator.ofFloat(0, 1);
  161. animator.setDuration(5000);
  162. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  163. @Override
  164. public void onAnimationUpdate(ValueAnimator animation) {
  165. progress = animation.getAnimatedFraction();
  166. update();
  167. invalidate();
  168. }
  169. });
  170. animator.addListener(new Animator.AnimatorListener() {
  171. @Override
  172. public void onAnimationStart(Animator arg0) {
  173. progress = 0;
  174. }
  175.  
  176. @Override
  177. public void onAnimationRepeat(Animator arg0) {
  178. }
  179.  
  180. @Override
  181. public void onAnimationEnd(Animator arg0) {
  182. progress = 1;
  183. }
  184.  
  185. @Override
  186. public void onAnimationCancel(Animator arg0) {
  187. progress = 1;
  188. }
  189. });
  190. animator.start();
  191. }
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. ---------------------------------
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. package photoembroidery.tat.radialprogressbar;
  222.  
  223. import android.os.Bundle;
  224. import android.support.v7.app.AppCompatActivity;
  225. import android.support.v7.widget.Toolbar;
  226. import android.view.View;
  227.  
  228.  
  229. public class MainActivity extends AppCompatActivity {
  230.  
  231. @Override
  232. protected void onCreate(Bundle savedInstanceState) {
  233. super.onCreate(savedInstanceState);
  234. setContentView(R.layout.activity_main);
  235. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  236. setSupportActionBar(toolbar);
  237.  
  238.  
  239. final RadialProgressBar rpb = (RadialProgressBar)findViewById(R.id.radialProgressBar);
  240.  
  241. rpb.setOnClickListener(new View.OnClickListener() {
  242. @Override
  243. public void onClick(View v) {
  244. rpb.animate_progress();
  245. }
  246. });
  247. }
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment