Advertisement
olcayertas

DonutProgress

Jun 25th, 2015
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.01 KB | None | 0 0
  1. /**
  2.  *
  3.  * Created by bruce on 14-10-30.
  4.  * Modified by Olcay Ertaş 11/05/15
  5.  *
  6.  */
  7. public class DonutProgress extends View {
  8.     private static final String LOG_TAG = "Namaz Vakti";
  9.     private static final String INSTANCE_STATE = "saved_instance";
  10.     private static final String INSTANCE_FINISHED_STROKE_COLOR = "finished_stroke_color";
  11.     private static final String INSTANCE_UNFINISHED_STROKE_COLOR = "unfinished_stroke_color";
  12.     private static final String INSTANCE_MAX = "max";
  13.     private static final String INSTANCE_PROGRESS = "progress";
  14.     private static final String INSTANCE_FINISHED_STROKE_WIDTH = "finished_stroke_width";
  15.     private static final String INSTANCE_UNFINISHED_STROKE_WIDTH = "unfinished_stroke_width";
  16.     private static final String INSTANCE_BACKGROUND_COLOR = "inner_background_color";
  17.     private final int defaultFinishedColor = Color.rgb(66, 145, 241);
  18.     private final int defaultUnfinishedColor = Color.rgb(204, 204, 204);
  19.     private Paint finishedPaint;
  20.     private Paint unfinishedPaint;
  21.     private Paint innerCirclePaint;
  22.     private RectF finishedOuterRect = new RectF();
  23.     private RectF unFinishedOuterRect = new RectF();
  24.     private float defaultStrokeWidth;
  25.     private float finishedStrokeWidth;
  26.     private float unfinishedStrokeWidth;
  27.     private float innerCircleRadius = 0;
  28.     private float oldInnerCircleRadius = 0;
  29.     private int minSize;
  30.     private int progress = 0;
  31.     private int max;
  32.     private int finishedStrokeColor;
  33.     private int unfinishedStrokeColor;
  34.     private int innerBackgroundColor;
  35.     private OnRadiusChangedListener onRadiusChangedListener;
  36.  
  37.     public DonutProgress(Context context) {
  38.         this(context, null);
  39.         minSize = (int) Utils.dp2px(getResources(), 100);
  40.         defaultStrokeWidth = Utils.dp2px(getResources(), 10);
  41.         initPainters();
  42.     }
  43.  
  44.     public DonutProgress(Context context, AttributeSet attrs) {
  45.         this(context, attrs, 0);
  46.         minSize = (int) Utils.dp2px(getResources(), 100);
  47.         defaultStrokeWidth = Utils.dp2px(getResources(), 10);
  48.         initPainters();
  49.     }
  50.  
  51.     public DonutProgress(Context context, AttributeSet attrs, int defStyleAttr) {
  52.         super(context, attrs, defStyleAttr);
  53.         init(context, attrs, defStyleAttr);
  54.         initPainters();
  55.     }
  56.  
  57.     private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  58.         minSize = (int) Utils.dp2px(getResources(), 100);
  59.         defaultStrokeWidth = Utils.dp2px(getResources(), 10);
  60.         final TypedArray attributes =
  61.                 context.getTheme().obtainStyledAttributes(
  62.                         attrs, R.styleable.DonutProgress, defStyleAttr, 0);
  63.         initByAttributes(attributes);
  64.         attributes.recycle();
  65.     }
  66.  
  67.     protected void initByAttributes(TypedArray attributes) {
  68.  
  69.         finishedStrokeColor = attributes.getColor(
  70.                 R.styleable.DonutProgress_donut_finished_color,
  71.                 defaultFinishedColor);
  72.  
  73.         unfinishedStrokeColor = attributes.getColor(
  74.                 R.styleable.DonutProgress_donut_unfinished_color,
  75.                 defaultUnfinishedColor);
  76.  
  77.         int defaultMax = 100;
  78.         setMax(attributes.getInt(R.styleable.DonutProgress_donut_max, defaultMax));
  79.         setProgress(attributes.getInt(R.styleable.DonutProgress_donut_progress, 0));
  80.         finishedStrokeWidth = attributes.getDimension(
  81.                 R.styleable.DonutProgress_donut_finished_stroke_width, defaultStrokeWidth);
  82.         unfinishedStrokeWidth = attributes.getDimension(
  83.                 R.styleable.DonutProgress_donut_unfinished_stroke_width, defaultStrokeWidth);
  84.  
  85.         int default_inner_background_color = Color.TRANSPARENT;
  86.         innerBackgroundColor = attributes.getColor(
  87.                 R.styleable.DonutProgress_donut_background_color, default_inner_background_color);
  88.     }
  89.  
  90.     protected void initPainters() {
  91.         finishedPaint = new Paint();
  92.         finishedPaint.setColor(finishedStrokeColor);
  93.         finishedPaint.setStyle(Paint.Style.STROKE);
  94.         finishedPaint.setAntiAlias(true);
  95.         finishedPaint.setStrokeWidth(finishedStrokeWidth);
  96.         finishedPaint.setStrokeCap(Paint.Cap.SQUARE);
  97.         unfinishedPaint = new Paint();
  98.         unfinishedPaint.setColor(unfinishedStrokeColor);
  99.         unfinishedPaint.setStyle(Paint.Style.STROKE);
  100.         unfinishedPaint.setAntiAlias(true);
  101.         unfinishedPaint.setStrokeWidth(unfinishedStrokeWidth);
  102.         innerCirclePaint = new Paint();
  103.         innerCirclePaint.setColor(Color.BLACK);
  104.         innerCirclePaint.setAlpha(70);
  105.         innerCirclePaint.setAntiAlias(true);
  106.     }
  107.  
  108.     public void setOnRadiusChangedListener(OnRadiusChangedListener listener) {
  109.         onRadiusChangedListener = listener;
  110.     }
  111.  
  112.     @SuppressWarnings("unused")
  113.     public float getInnerCircleRadius() {
  114.         return innerCircleRadius;
  115.     }
  116.  
  117.     @Override
  118.     public void invalidate() {
  119.         initPainters();
  120.         super.invalidate();
  121.     }
  122.  
  123.     @SuppressWarnings("unused")
  124.     public void setInnerCircleRadius(float innerCircleRadius) {
  125.         this.innerCircleRadius = innerCircleRadius;
  126.     }
  127.  
  128.     public interface OnRadiusChangedListener {
  129.         void onRadiusChange(float radius);
  130.     }
  131.  
  132.     public float getFinishedStrokeWidth() {
  133.         return finishedStrokeWidth;
  134.     }
  135.  
  136.     @SuppressWarnings("unused")
  137.     public void setFinishedStrokeWidth(float finishedStrokeWidth) {
  138.         this.finishedStrokeWidth = finishedStrokeWidth;
  139.         this.invalidate();
  140.     }
  141.  
  142.     public float getUnfinishedStrokeWidth() {
  143.         return unfinishedStrokeWidth;
  144.     }
  145.  
  146.     @SuppressWarnings("unused")
  147.     public void setUnfinishedStrokeWidth(float unfinishedStrokeWidth) {
  148.         this.unfinishedStrokeWidth = unfinishedStrokeWidth;
  149.         this.invalidate();
  150.     }
  151.  
  152.     private float getProgressAngle() {
  153.         return progress / (float) max * 360f;
  154.     }
  155.  
  156.     public int getProgress() {
  157.         return progress;
  158.     }
  159.  
  160.     public void setProgress(int progress) {
  161.         this.progress = progress;
  162.  
  163.         if (this.progress > max) {
  164.             this.progress %= max;
  165.         }
  166.  
  167.         invalidate();
  168.     }
  169.  
  170.     public int getMax() {
  171.         return max;
  172.     }
  173.  
  174.     public void setMax(int max) {
  175.  
  176.         if (max > 0) {
  177.             this.max = max;
  178.             invalidate();
  179.         }
  180.     }
  181.  
  182.     public int getFinishedStrokeColor() {
  183.         return finishedStrokeColor;
  184.     }
  185.  
  186.     @SuppressWarnings("unused")
  187.     public void setFinishedStrokeColor(int finishedStrokeColor) {
  188.         this.finishedStrokeColor = finishedStrokeColor;
  189.         this.invalidate();
  190.     }
  191.  
  192.     public int getUnfinishedStrokeColor() {
  193.         return unfinishedStrokeColor;
  194.     }
  195.  
  196.     @SuppressWarnings("unused")
  197.     public void setUnfinishedStrokeColor(int unfinishedStrokeColor) {
  198.         this.unfinishedStrokeColor = unfinishedStrokeColor;
  199.         this.invalidate();
  200.     }
  201.  
  202.     public int getInnerBackgroundColor() {
  203.         return innerBackgroundColor;
  204.     }
  205.  
  206.     @SuppressWarnings("unused")
  207.     public void setInnerBackgroundColor(int innerBackgroundColor) {
  208.         this.innerBackgroundColor = innerBackgroundColor;
  209.         this.invalidate();
  210.     }
  211.  
  212.     @Override
  213.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  214.         setMeasuredDimension(measure(widthMeasureSpec), measure(heightMeasureSpec));
  215.     }
  216.  
  217.     private int measure(int measureSpec) {
  218.         int result;
  219.         int mode = MeasureSpec.getMode(measureSpec);
  220.         int size = MeasureSpec.getSize(measureSpec);
  221.  
  222.         if (mode == MeasureSpec.EXACTLY) {
  223.             result = size;
  224.         } else {
  225.             result = minSize;
  226.  
  227.             if (mode == MeasureSpec.AT_MOST) {
  228.                 result = Math.min(result, size);
  229.             }
  230.         }
  231.  
  232.         return result;
  233.     }
  234.  
  235.     @Override
  236.     protected void onDraw(Canvas canvas) {
  237.         super.onDraw(canvas);
  238.  
  239.         int vh = getHeight();
  240.         int vw = getWidth();
  241.  
  242.         float top;
  243.         float bottom;
  244.         float left;
  245.         float right;
  246.  
  247.         if (vw > vh) {
  248.             top = vh * 0.1f;
  249.             bottom = vh - top;
  250.             left = (vw - vh * 0.8f) / 2;
  251.             right = vw - left;
  252.         } else {
  253.             top = (vh - vw * 0.8f) / 2;
  254.             bottom = vh - top;
  255.             left = vw * 0.1f;
  256.             right = vw - left;
  257.         }
  258.  
  259.         innerCircleRadius = Math.abs((left - right)) / 2f;
  260.  
  261.         if (oldInnerCircleRadius == 0) {
  262.             oldInnerCircleRadius = innerCircleRadius;
  263.  
  264.             if (onRadiusChangedListener != null)
  265.                 onRadiusChangedListener.onRadiusChange(innerCircleRadius);
  266.         } else if (innerCircleRadius - oldInnerCircleRadius > innerCircleRadius * 0.1f) {
  267.             oldInnerCircleRadius = innerCircleRadius;
  268.  
  269.             if (onRadiusChangedListener != null)
  270.                 onRadiusChangedListener.onRadiusChange(innerCircleRadius);
  271.         }
  272.  
  273.         finishedOuterRect.set(left, top, right, bottom);
  274.         unFinishedOuterRect.set(left, top, right, bottom);
  275.         canvas.drawCircle((float) vw / 2, (float) vh / 2, innerCircleRadius, innerCirclePaint);
  276.         float pa = getProgressAngle();
  277.         unfinishedPaint.setAlpha(90);
  278.         canvas.drawArc(unFinishedOuterRect, pa + 270, 360 - pa, false, unfinishedPaint);
  279.         canvas.drawArc(finishedOuterRect, 270, progress / (float) max * 360f, false, finishedPaint);
  280.     }
  281.  
  282.     @Override
  283.     protected Parcelable onSaveInstanceState() {
  284.         final Bundle bundle = new Bundle();
  285.         bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
  286.         bundle.putInt(INSTANCE_FINISHED_STROKE_COLOR, getFinishedStrokeColor());
  287.         bundle.putInt(INSTANCE_UNFINISHED_STROKE_COLOR, getUnfinishedStrokeColor());
  288.         bundle.putInt(INSTANCE_MAX, getMax());
  289.         bundle.putInt(INSTANCE_PROGRESS, getProgress());
  290.         bundle.putFloat(INSTANCE_FINISHED_STROKE_WIDTH, getFinishedStrokeWidth());
  291.         bundle.putFloat(INSTANCE_UNFINISHED_STROKE_WIDTH, getUnfinishedStrokeWidth());
  292.         bundle.putInt(INSTANCE_BACKGROUND_COLOR, getInnerBackgroundColor());
  293.         return bundle;
  294.     }
  295.  
  296.     @Override
  297.     protected void onRestoreInstanceState(Parcelable state) {
  298.  
  299.         if (state instanceof Bundle) {
  300.             final Bundle bundle = (Bundle) state;
  301.             finishedStrokeColor = bundle.getInt(INSTANCE_FINISHED_STROKE_COLOR);
  302.             unfinishedStrokeColor = bundle.getInt(INSTANCE_UNFINISHED_STROKE_COLOR);
  303.             finishedStrokeWidth = bundle.getFloat(INSTANCE_FINISHED_STROKE_WIDTH);
  304.             unfinishedStrokeWidth = bundle.getFloat(INSTANCE_UNFINISHED_STROKE_WIDTH);
  305.             innerBackgroundColor = bundle.getInt(INSTANCE_BACKGROUND_COLOR);
  306.             initPainters();
  307.             setMax(bundle.getInt(INSTANCE_MAX));
  308.             setProgress(bundle.getInt(INSTANCE_PROGRESS));
  309.             super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
  310.             return;
  311.         }
  312.  
  313.         super.onRestoreInstanceState(state);
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement