Skip to content

Commit

Permalink
Ability to set custom colors dynamically or from the xml layout attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
jpardogo committed Apr 14, 2014
1 parent ef081a0 commit d8d3c5a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class MainActivity extends ListActivity {

private static final long REFRESH_TIME = 4000;
private int LIST_ITEM_COUNT=40;
@InjectView(R.id.google_progress)
GoogleProgressBar mProgressBar;
Expand All @@ -30,7 +31,9 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
/**Dynamically*/
// mProgressBar.setIndeterminateDrawable(new FoldingCirclesDrawable.Builder(this).build());
// mProgressBar.setIndeterminateDrawable(new FoldingCirclesDrawable.Builder(this)
// .colors(getResources().getIntArray(R.array.rainbow))
// .build());
refresh();

}
Expand All @@ -48,7 +51,7 @@ public void run() {
setListAdapter(new ArrayAdapter<String>(getBaseContext(), R.layout.item_list, getListItem()));

}
},4000);
},REFRESH_TIME);
}

private ArrayList<String> getListItem() {
Expand Down
3 changes: 2 additions & 1 deletion example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
gpb:type="folding_circles"/>
gpb:type="folding_circles"
gpb:colors="@array/rainbow"/>

<!--If we add the google progress dynamically we use a native ProgressBar-->
<!--<ProgressBar-->
Expand Down
10 changes: 10 additions & 0 deletions example/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<integer-array name="rainbow">
<item>@color/darkblue</item>
<item>@color/darkpurple</item>
<item>@color/darkgreen</item>
<item>@color/darkorange</item>
</integer-array>
</resources>
7 changes: 7 additions & 0 deletions example/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="darkblue">#FF0099CC</color>
<color name="darkpurple">#FF9933CC</color>
<color name="darkgreen">#FF669900</color>
<color name="darkorange">#FFFF8800</color>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,26 @@ public class FoldingCirclesDrawable extends Drawable implements Drawable.Callbac
private int mAxisValue;
private int mAlpha = ALPHA_OPAQUE;
private ColorFilter mColorFilter;

private enum ProgressColors {

RED(0xFFC93437),
BLUE(0xFF375BF1),
YELLOW(0xFFF7D23E),
GREEN(0xFF34A350);

private int color;

private ProgressColors(final int color) {
this.color = color;
}
}

private static int mColor1;
private static int mColor2;
private static int mColor3;
private static int mColor4;
private int fstColor, scndColor;
private boolean goesBackward;

private enum ProgressStates {

FOLDING_DOWN(ProgressColors.RED, ProgressColors.BLUE, false),
FOLDING_LEFT(ProgressColors.RED, ProgressColors.YELLOW, true),
FOLDING_UP(ProgressColors.YELLOW, ProgressColors.GREEN, true),
FOLDING_RIGHT(ProgressColors.BLUE, ProgressColors.GREEN, false);

private ProgressColors fstColor, scndColor;
private boolean goesBackward;

ProgressStates(ProgressColors fstColor, ProgressColors scndColor, boolean goesBackward) {
this.fstColor = fstColor;
this.scndColor = scndColor;
this.goesBackward = goesBackward;
}
FOLDING_DOWN,
FOLDING_LEFT,
FOLDING_UP,
FOLDING_RIGHT
}

public FoldingCirclesDrawable() {
initCirclesProgress();
public FoldingCirclesDrawable(int[] colors) {
initCirclesProgress(colors);
}

private void initCirclesProgress() {
private void initCirclesProgress(int[] colors) {
initColors(colors);
mPath = new Path();

Paint basePaint = new Paint();
Expand All @@ -89,6 +71,13 @@ private void initCirclesProgress() {
setColorFilter(mColorFilter);
}

private void initColors(int[] colors) {
mColor1=colors[0];
mColor2=colors[1];
mColor3=colors[2];
mColor4=colors[3];
}

@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
Expand All @@ -107,18 +96,19 @@ protected boolean onLevelChange(int level) {
mCurrentState = ProgressStates.values()[stateForLevel];

// colors
resetColor(mCurrentState);
int levelForCircle = (int) (animationLevel % MAX_LEVEL_PER_CIRCLE);

boolean halfPassed;
if (!mCurrentState.goesBackward) {
if (!goesBackward) {
halfPassed = levelForCircle != (int) (animationLevel % (MAX_LEVEL_PER_CIRCLE / 2));
} else {
halfPassed = levelForCircle == (int) (animationLevel % (MAX_LEVEL_PER_CIRCLE / 2));
levelForCircle = (int) (MAX_LEVEL_PER_CIRCLE - levelForCircle);
}

mFstHalfPaint.setColor(mCurrentState.fstColor.color);
mScndHalfPaint.setColor(mCurrentState.scndColor.color);
mFstHalfPaint.setColor(fstColor);
mScndHalfPaint.setColor(scndColor);

if (!halfPassed) {
mAbovePaint.setColor(mScndHalfPaint.getColor());
Expand All @@ -136,6 +126,31 @@ protected boolean onLevelChange(int level) {
return true;
}

private void resetColor(ProgressStates currentState) {
switch (currentState){
case FOLDING_DOWN:
fstColor= mColor1;
scndColor=mColor2;
goesBackward=false;
break;
case FOLDING_LEFT:
fstColor= mColor1;
scndColor=mColor3;
goesBackward=true;
break;
case FOLDING_UP:
fstColor= mColor3;
scndColor=mColor4;
goesBackward=true;
break;
case FOLDING_RIGHT:
fstColor=mColor2;
scndColor=mColor4;
goesBackward=false;
break;
}
}

@Override
public void draw(Canvas canvas) {
if (mCurrentState != null) {
Expand Down Expand Up @@ -186,18 +201,15 @@ private void drawYMotion(Canvas canvas) {
@Override
public void setAlpha(int alpha) {
this.mAlpha = alpha;

mFstHalfPaint.setAlpha(alpha);
mScndHalfPaint.setAlpha(alpha);

int targetAboveAlpha = (ALPHA_ABOVE_DEFAULT * alpha) / ALPHA_OPAQUE;
mAbovePaint.setAlpha(targetAboveAlpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
this.mColorFilter = cf;

mFstHalfPaint.setColorFilter(cf);
mScndHalfPaint.setColorFilter(cf);
mAbovePaint.setColorFilter(cf);
Expand Down Expand Up @@ -233,17 +245,28 @@ public void unscheduleDrawable(Drawable who, Runnable what) {
}

public static class Builder {
private int[] mColors;

public Builder(Context context){
initDefaults(context);
}

private void initDefaults(Context context) {
//Default values
mColors = context.getResources().getIntArray(R.array.google_colors);
}

public Builder colors(int[] colors) {
if (colors == null || colors.length == 0) {
throw new IllegalArgumentException("Your color array must contains at least 4 values");
}

mColors = colors;
return this;
}

public Drawable build() {
return new FoldingCirclesDrawable();
return new FoldingCirclesDrawable(mColors);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ public GoogleProgressBar(Context context, AttributeSet attrs) {
public GoogleProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GoogleProgressBar, defStyle, 0);
final int typeIndex = a.getInteger(R.styleable.GoogleProgressBar_type, 0);
final int typeIndex = a.getInteger(R.styleable.GoogleProgressBar_type, context.getResources().getInteger(R.integer.default_type));
final int colorsId = a.getResourceId(R.styleable.GoogleProgressBar_colors, R.array.google_colors);
a.recycle();

Drawable drawable = buildDrawable(context,typeIndex);
Drawable drawable = buildDrawable(context,typeIndex,colorsId);
if(drawable!=null)
setIndeterminateDrawable(drawable);
}

private Drawable buildDrawable(Context context, int typeIndex) {
private Drawable buildDrawable(Context context, int typeIndex,int colorsId) {
Drawable drawable = null;
ProgressType type = ProgressType.values()[typeIndex];
switch (type){
case FOLDING_CIRCLES:
FoldingCirclesDrawable.Builder builder = new FoldingCirclesDrawable.Builder(context);
builder.colors(getResources().getIntArray(colorsId));
drawable = builder.build();
break;
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<attr name="type" format="enum">
<enum name="folding_circles" value="0" />
</attr>
<attr name="colors" format="reference"/>
</declare-styleable>
</resources>
7 changes: 7 additions & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#FFC93437</color>
<color name="blue">#FF375BF1</color>
<color name="yellow">#FFF7D23E</color>
<color name="green">#FF34A350</color>
</resources>
10 changes: 10 additions & 0 deletions library/src/main/res/values/defaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="google_colors">
<item>@color/red</item>
<item>@color/blue</item>
<item>@color/yellow</item>
<item>@color/green</item>
</integer-array>
<integer name="default_type">0</integer>
</resources>

0 comments on commit d8d3c5a

Please sign in to comment.