Skip to content

add fresco progress drawable #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,19 @@ public class CircularProgressDrawable extends Drawable {

@Override
public void draw(Canvas canvas) {
final Rect bounds = getBounds();
draw(canvas, 0);
}

// Calculations on the different components sizes
int size = Math.min(bounds.height(), bounds.width());
/**
*
* @param canvas
* @param size of the circle. If <= 0 then circle fill whole canvas
*/
protected void draw(Canvas canvas, int size) {
final Rect bounds = getBounds();
if(size <= 0){
size = Math.min(bounds.height(), bounds.width());
}
float outerRadius = (size / 2) - (ringWidth / 2);
float innerRadius = outerRadius * circleScale;
float offsetX = (bounds.width() - outerRadius * 2) / 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.sefford.circularprogressdrawable;

import android.graphics.Canvas;

/**
* Created by Ilya Eremin on 25.04.2015.
*/
public class FrescoCircularProgressDrawable extends CircularProgressDrawable {

private final int size;

/**
* Creates a new CouponDrawable.
*
* @param ringWidth Width of the filled ring
* @param circleScale Scale difference between the outer ring and the inner circle
* @param outlineColor Color for the outline color
* @param ringColor Color for the filled ring
* @param centerColor Color for the center element
*/
public FrescoCircularProgressDrawable(int ringWidth, float circleScale, int outlineColor, int ringColor, int centerColor, int size) {
super(ringWidth, circleScale, outlineColor, ringColor, centerColor);
this.size = size;
}

@Override protected boolean onLevelChange(int level) {
if (indeterminate) {
this.progress = (float)level/10000;
} else {
this.progress = PROGRESS_FACTOR * ((float)level/10000);
}
invalidateSelf();
return super.onLevelChange(level);
}

@Override public void draw(Canvas canvas) {
draw(canvas, size);
}

/**
* Helper class to manage the creation of a CircularProgressDrawable
*
* @author Saul Diaz <[email protected]>
*/
public static class Builder {

/**
* Witdh of the stroke of the filled ring
*/
int ringWidth;
/**
* Color of the outline of the empty ring in #AARRGGBB mode.
*/
int outlineColor;
/**
* Color of the filled ring in #AARRGGBB mode.
*/
int ringColor;
/**
* Color of the inner circle in #AARRGGBB mode.
*/
int centerColor;
/**
* Scale between the outer ring and the inner circle
*/
float circleScale = 0.75f;

/**
* Circle size in pixels
*/
int size;

/**
* Sets the ring width.
*
* @param ringWidth Default ring width
* @return This builder
*/
public Builder setRingWidth(int ringWidth) {
this.ringWidth = ringWidth;
return this;
}

/**
* Sets the default empty outer ring outline color.
*
* @param outlineColor Outline color in #AARRGGBB format.
* @return
*/
public Builder setOutlineColor(int outlineColor) {
this.outlineColor = outlineColor;
return this;
}

/**
* Sets the progress ring color.
*
* @param ringColor Ring color in #AARRGGBB format.
* @returns This Builder
*/
public Builder setRingColor(int ringColor) {
this.ringColor = ringColor;
return this;
}


/**
* Sets the inner circle color.
*
* @param centerColor Inner circle color in #AARRGGBB format.
* @return This builder
*/
public Builder setCenterColor(int centerColor) {
this.centerColor = centerColor;
return this;
}

/**
* Sets the inner circle scale. Defaults to 0.75.
*
* @param circleScale Inner circle scale.
* @return This builder
*/
public Builder setInnerCircleScale(float circleScale) {
this.circleScale = circleScale;
return this;
}


/**
* Sets the size of the circle in pixels. Default fill whole view
*
* @param size in pixels
* @return This builder
*/
public Builder setSize(int size){
this.size = size;
return this;
}

/**
* Creates a new CircularProgressDrawable with the requested parameters
*
* @return New CircularProgressDrawableInstance
*/
public FrescoCircularProgressDrawable create() {
return new FrescoCircularProgressDrawable(ringWidth, circleScale, outlineColor, ringColor, centerColor, size);
}

}
}