Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Created by bruce on 14-10-30.
- * Modified by Olcay Ertaş 11/05/15
- *
- */
- public class DonutProgress extends View {
- private static final String LOG_TAG = "Namaz Vakti";
- private static final String INSTANCE_STATE = "saved_instance";
- private static final String INSTANCE_FINISHED_STROKE_COLOR = "finished_stroke_color";
- private static final String INSTANCE_UNFINISHED_STROKE_COLOR = "unfinished_stroke_color";
- private static final String INSTANCE_MAX = "max";
- private static final String INSTANCE_PROGRESS = "progress";
- private static final String INSTANCE_FINISHED_STROKE_WIDTH = "finished_stroke_width";
- private static final String INSTANCE_UNFINISHED_STROKE_WIDTH = "unfinished_stroke_width";
- private static final String INSTANCE_BACKGROUND_COLOR = "inner_background_color";
- private final int defaultFinishedColor = Color.rgb(66, 145, 241);
- private final int defaultUnfinishedColor = Color.rgb(204, 204, 204);
- private Paint finishedPaint;
- private Paint unfinishedPaint;
- private Paint innerCirclePaint;
- private RectF finishedOuterRect = new RectF();
- private RectF unFinishedOuterRect = new RectF();
- private float defaultStrokeWidth;
- private float finishedStrokeWidth;
- private float unfinishedStrokeWidth;
- private float innerCircleRadius = 0;
- private float oldInnerCircleRadius = 0;
- private int minSize;
- private int progress = 0;
- private int max;
- private int finishedStrokeColor;
- private int unfinishedStrokeColor;
- private int innerBackgroundColor;
- private OnRadiusChangedListener onRadiusChangedListener;
- public DonutProgress(Context context) {
- this(context, null);
- minSize = (int) Utils.dp2px(getResources(), 100);
- defaultStrokeWidth = Utils.dp2px(getResources(), 10);
- initPainters();
- }
- public DonutProgress(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- minSize = (int) Utils.dp2px(getResources(), 100);
- defaultStrokeWidth = Utils.dp2px(getResources(), 10);
- initPainters();
- }
- public DonutProgress(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context, attrs, defStyleAttr);
- initPainters();
- }
- private void init(Context context, AttributeSet attrs, int defStyleAttr) {
- minSize = (int) Utils.dp2px(getResources(), 100);
- defaultStrokeWidth = Utils.dp2px(getResources(), 10);
- final TypedArray attributes =
- context.getTheme().obtainStyledAttributes(
- attrs, R.styleable.DonutProgress, defStyleAttr, 0);
- initByAttributes(attributes);
- attributes.recycle();
- }
- protected void initByAttributes(TypedArray attributes) {
- finishedStrokeColor = attributes.getColor(
- R.styleable.DonutProgress_donut_finished_color,
- defaultFinishedColor);
- unfinishedStrokeColor = attributes.getColor(
- R.styleable.DonutProgress_donut_unfinished_color,
- defaultUnfinishedColor);
- int defaultMax = 100;
- setMax(attributes.getInt(R.styleable.DonutProgress_donut_max, defaultMax));
- setProgress(attributes.getInt(R.styleable.DonutProgress_donut_progress, 0));
- finishedStrokeWidth = attributes.getDimension(
- R.styleable.DonutProgress_donut_finished_stroke_width, defaultStrokeWidth);
- unfinishedStrokeWidth = attributes.getDimension(
- R.styleable.DonutProgress_donut_unfinished_stroke_width, defaultStrokeWidth);
- int default_inner_background_color = Color.TRANSPARENT;
- innerBackgroundColor = attributes.getColor(
- R.styleable.DonutProgress_donut_background_color, default_inner_background_color);
- }
- protected void initPainters() {
- finishedPaint = new Paint();
- finishedPaint.setColor(finishedStrokeColor);
- finishedPaint.setStyle(Paint.Style.STROKE);
- finishedPaint.setAntiAlias(true);
- finishedPaint.setStrokeWidth(finishedStrokeWidth);
- finishedPaint.setStrokeCap(Paint.Cap.SQUARE);
- unfinishedPaint = new Paint();
- unfinishedPaint.setColor(unfinishedStrokeColor);
- unfinishedPaint.setStyle(Paint.Style.STROKE);
- unfinishedPaint.setAntiAlias(true);
- unfinishedPaint.setStrokeWidth(unfinishedStrokeWidth);
- innerCirclePaint = new Paint();
- innerCirclePaint.setColor(Color.BLACK);
- innerCirclePaint.setAlpha(70);
- innerCirclePaint.setAntiAlias(true);
- }
- public void setOnRadiusChangedListener(OnRadiusChangedListener listener) {
- onRadiusChangedListener = listener;
- }
- @SuppressWarnings("unused")
- public float getInnerCircleRadius() {
- return innerCircleRadius;
- }
- @Override
- public void invalidate() {
- initPainters();
- super.invalidate();
- }
- @SuppressWarnings("unused")
- public void setInnerCircleRadius(float innerCircleRadius) {
- this.innerCircleRadius = innerCircleRadius;
- }
- public interface OnRadiusChangedListener {
- void onRadiusChange(float radius);
- }
- public float getFinishedStrokeWidth() {
- return finishedStrokeWidth;
- }
- @SuppressWarnings("unused")
- public void setFinishedStrokeWidth(float finishedStrokeWidth) {
- this.finishedStrokeWidth = finishedStrokeWidth;
- this.invalidate();
- }
- public float getUnfinishedStrokeWidth() {
- return unfinishedStrokeWidth;
- }
- @SuppressWarnings("unused")
- public void setUnfinishedStrokeWidth(float unfinishedStrokeWidth) {
- this.unfinishedStrokeWidth = unfinishedStrokeWidth;
- this.invalidate();
- }
- private float getProgressAngle() {
- return progress / (float) max * 360f;
- }
- public int getProgress() {
- return progress;
- }
- public void setProgress(int progress) {
- this.progress = progress;
- if (this.progress > max) {
- this.progress %= max;
- }
- invalidate();
- }
- public int getMax() {
- return max;
- }
- public void setMax(int max) {
- if (max > 0) {
- this.max = max;
- invalidate();
- }
- }
- public int getFinishedStrokeColor() {
- return finishedStrokeColor;
- }
- @SuppressWarnings("unused")
- public void setFinishedStrokeColor(int finishedStrokeColor) {
- this.finishedStrokeColor = finishedStrokeColor;
- this.invalidate();
- }
- public int getUnfinishedStrokeColor() {
- return unfinishedStrokeColor;
- }
- @SuppressWarnings("unused")
- public void setUnfinishedStrokeColor(int unfinishedStrokeColor) {
- this.unfinishedStrokeColor = unfinishedStrokeColor;
- this.invalidate();
- }
- public int getInnerBackgroundColor() {
- return innerBackgroundColor;
- }
- @SuppressWarnings("unused")
- public void setInnerBackgroundColor(int innerBackgroundColor) {
- this.innerBackgroundColor = innerBackgroundColor;
- this.invalidate();
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(measure(widthMeasureSpec), measure(heightMeasureSpec));
- }
- private int measure(int measureSpec) {
- int result;
- int mode = MeasureSpec.getMode(measureSpec);
- int size = MeasureSpec.getSize(measureSpec);
- if (mode == MeasureSpec.EXACTLY) {
- result = size;
- } else {
- result = minSize;
- if (mode == MeasureSpec.AT_MOST) {
- result = Math.min(result, size);
- }
- }
- return result;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- int vh = getHeight();
- int vw = getWidth();
- float top;
- float bottom;
- float left;
- float right;
- if (vw > vh) {
- top = vh * 0.1f;
- bottom = vh - top;
- left = (vw - vh * 0.8f) / 2;
- right = vw - left;
- } else {
- top = (vh - vw * 0.8f) / 2;
- bottom = vh - top;
- left = vw * 0.1f;
- right = vw - left;
- }
- innerCircleRadius = Math.abs((left - right)) / 2f;
- if (oldInnerCircleRadius == 0) {
- oldInnerCircleRadius = innerCircleRadius;
- if (onRadiusChangedListener != null)
- onRadiusChangedListener.onRadiusChange(innerCircleRadius);
- } else if (innerCircleRadius - oldInnerCircleRadius > innerCircleRadius * 0.1f) {
- oldInnerCircleRadius = innerCircleRadius;
- if (onRadiusChangedListener != null)
- onRadiusChangedListener.onRadiusChange(innerCircleRadius);
- }
- finishedOuterRect.set(left, top, right, bottom);
- unFinishedOuterRect.set(left, top, right, bottom);
- canvas.drawCircle((float) vw / 2, (float) vh / 2, innerCircleRadius, innerCirclePaint);
- float pa = getProgressAngle();
- unfinishedPaint.setAlpha(90);
- canvas.drawArc(unFinishedOuterRect, pa + 270, 360 - pa, false, unfinishedPaint);
- canvas.drawArc(finishedOuterRect, 270, progress / (float) max * 360f, false, finishedPaint);
- }
- @Override
- protected Parcelable onSaveInstanceState() {
- final Bundle bundle = new Bundle();
- bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
- bundle.putInt(INSTANCE_FINISHED_STROKE_COLOR, getFinishedStrokeColor());
- bundle.putInt(INSTANCE_UNFINISHED_STROKE_COLOR, getUnfinishedStrokeColor());
- bundle.putInt(INSTANCE_MAX, getMax());
- bundle.putInt(INSTANCE_PROGRESS, getProgress());
- bundle.putFloat(INSTANCE_FINISHED_STROKE_WIDTH, getFinishedStrokeWidth());
- bundle.putFloat(INSTANCE_UNFINISHED_STROKE_WIDTH, getUnfinishedStrokeWidth());
- bundle.putInt(INSTANCE_BACKGROUND_COLOR, getInnerBackgroundColor());
- return bundle;
- }
- @Override
- protected void onRestoreInstanceState(Parcelable state) {
- if (state instanceof Bundle) {
- final Bundle bundle = (Bundle) state;
- finishedStrokeColor = bundle.getInt(INSTANCE_FINISHED_STROKE_COLOR);
- unfinishedStrokeColor = bundle.getInt(INSTANCE_UNFINISHED_STROKE_COLOR);
- finishedStrokeWidth = bundle.getFloat(INSTANCE_FINISHED_STROKE_WIDTH);
- unfinishedStrokeWidth = bundle.getFloat(INSTANCE_UNFINISHED_STROKE_WIDTH);
- innerBackgroundColor = bundle.getInt(INSTANCE_BACKGROUND_COLOR);
- initPainters();
- setMax(bundle.getInt(INSTANCE_MAX));
- setProgress(bundle.getInt(INSTANCE_PROGRESS));
- super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
- return;
- }
- super.onRestoreInstanceState(state);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement