Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package photoembroidery.tat.radialprogressbar;
- import android.animation.Animator;
- import android.animation.ValueAnimator;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.SweepGradient;
- import android.util.AttributeSet;
- import android.view.View;
- public class RadialProgressBar extends View {
- static final int COLOR_BACKGROUNDPURPLE = Color.argb(0xFF, 32, 24, 79);
- static final int COLOR_LIGHTPURPLE = Color.argb(0xFF, 98, 66, 141);
- static final int TICKS = 200;
- float progress = 0;
- Paint linePaint;
- Paint textPaint;
- Paint circlePaint;
- boolean dirty = true;
- float[] lines;
- int drawlines = TICKS;
- float textX, textY;
- float cx, cy, cr;
- public RadialProgressBar(Context context) {
- super(context);
- }
- public RadialProgressBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public RadialProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- public void update() {
- float width = getWidth();
- float height = getHeight();
- float dim = Math.min(width, height);
- drawlines = (int) (TICKS * progress * 4);
- SweepGradient sweep = new SweepGradient(dim / 2, height / 2, new int[]{COLOR_LIGHTPURPLE, Color.WHITE}, new float[]{0, progress});
- linePaint.setShader(sweep);
- double a = (Math.PI * 2) * progress;
- float circlePositionRadius = (dim * .95f) / 2;
- cx = (float) ((dim / 2) + Math.cos(a) * circlePositionRadius);
- cy = (float) ((height / 2) + Math.sin(a) * circlePositionRadius);
- textX = ((dim - textPaint.measureText(getProgressText())) / 2);
- textY = ((height + textPaint.measureText("a")) / 2);
- }
- public void init() {
- if (textPaint == null) textPaint = new Paint();
- if (linePaint == null) linePaint = new Paint();
- if (circlePaint == null) circlePaint = new Paint();
- float width = getWidth();
- float height = getHeight();
- float dim = Math.min(width, height);
- textPaint.setTextSize((int) (dim / 7));
- textPaint.setColor(COLOR_LIGHTPURPLE);
- linePaint.setStyle(Paint.Style.STROKE);
- linePaint.setStrokeWidth(10);
- circlePaint.setStyle(Paint.Style.STROKE);
- circlePaint.setStrokeWidth(10);
- circlePaint.setColor(Color.WHITE);
- if (lines == null) {
- lines = new float[4 * TICKS];
- }
- double a = 0;
- double step = ((2 * Math.PI) / TICKS);
- float r0 = (dim * .9f) / 2;
- float r1 = (dim / 2);
- for (int i = 0, s = lines.length; i < s; i += 4) {
- a += step;
- lines[i] = (float) ((dim / 2) + Math.cos(a) * r0);
- lines[i + 1] = (float) ((height / 2) + Math.sin(a) * r0);
- lines[i + 2] = (float) ((dim / 2) + Math.cos(a) * r1);
- lines[i + 3] = (float) ((height / 2) + Math.sin(a) * r1);
- }
- cr = (dim * 0.1f) / 2;
- dirty = false;
- update();
- }
- private String getProgressText() {
- return String.format("%.2f%%", 100 * progress);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (dirty) init();
- canvas.drawColor(COLOR_BACKGROUNDPURPLE);
- canvas.drawLines(lines, 0, drawlines, linePaint);
- canvas.drawText(getProgressText(), textX, textY, textPaint);
- canvas.drawCircle(cx, cy, cr, circlePaint);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int desiredWidth = getResources().getDimensionPixelSize(R.dimen.preferSize);
- int desiredHeight = desiredWidth;
- int widthMode = MeasureSpec.getMode(widthMeasureSpec);
- int widthSize = MeasureSpec.getSize(widthMeasureSpec);
- int heightMode = MeasureSpec.getMode(heightMeasureSpec);
- int heightSize = MeasureSpec.getSize(heightMeasureSpec);
- int width;
- int height;
- //Measure Width
- if (widthMode == MeasureSpec.EXACTLY) {
- //Must be this size
- width = widthSize;
- } else if (widthMode == MeasureSpec.AT_MOST) {
- //Can't be bigger than...
- width = Math.min(desiredWidth, widthSize);
- } else {
- //Be whatever you want
- width = desiredWidth;
- }
- //Measure Height
- if (heightMode == MeasureSpec.EXACTLY) {
- //Must be this size
- height = heightSize;
- } else if (heightMode == MeasureSpec.AT_MOST) {
- //Can't be bigger than...
- height = Math.min(desiredHeight, heightSize);
- } else {
- //Be whatever you want
- height = desiredHeight;
- }
- //MUST CALL THIS
- setMeasuredDimension(width, height);
- }
- public float getProgress() {
- return progress;
- }
- public void setProgress(float progress) {
- this.progress = progress;
- dirty = true;
- }
- ValueAnimator animator;
- public void animate_progress() {
- animator = ValueAnimator.ofFloat(0, 1);
- animator.setDuration(5000);
- animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override
- public void onAnimationUpdate(ValueAnimator animation) {
- progress = animation.getAnimatedFraction();
- update();
- invalidate();
- }
- });
- animator.addListener(new Animator.AnimatorListener() {
- @Override
- public void onAnimationStart(Animator arg0) {
- progress = 0;
- }
- @Override
- public void onAnimationRepeat(Animator arg0) {
- }
- @Override
- public void onAnimationEnd(Animator arg0) {
- progress = 1;
- }
- @Override
- public void onAnimationCancel(Animator arg0) {
- progress = 1;
- }
- });
- animator.start();
- }
- }
- ---------------------------------
- package photoembroidery.tat.radialprogressbar;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- final RadialProgressBar rpb = (RadialProgressBar)findViewById(R.id.radialProgressBar);
- rpb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- rpb.animate_progress();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment