Skip to content

Commit

Permalink
Added ability to set custome types for active and inactive circles. W…
Browse files Browse the repository at this point in the history
…ith different colors
  • Loading branch information
Matt Oakes committed Aug 21, 2011
1 parent bc71a12 commit fc68dd1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
3 changes: 2 additions & 1 deletion viewflow-example/res/layout/circle_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/viewflowindic"
android:layout_gravity="center_horizontal" />
android:layout_gravity="center_horizontal"
app:inactiveType="fill" />
<org.taptwo.android.widget.ViewFlow
android:id="@+id/viewflow" android:layout_width="fill_parent"
android:layout_height="fill_parent" app:sidebuffer="3"></org.taptwo.android.widget.ViewFlow>
Expand Down
8 changes: 6 additions & 2 deletions viewflow/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
<attr name="sidebuffer" format="integer" />
</declare-styleable>
<declare-styleable name="CircleFlowIndicator">
<attr name="fillColor" format="color" />
<attr name="strokeColor" format="color" />
<attr name="activeColor" format="color" />
<attr name="inactiveColor" format="color" />
<attr name="radius" format="dimension" />
<attr name="centered" format="boolean" />
<attr name="inactiveType">
<flag name="stroke" value="0" />
<flag name="fill" value="1" />
</attr>
<attr name="activeType">
<flag name="stroke" value="0" />
<flag name="fill" value="1" />
</attr>
</declare-styleable>
<declare-styleable name="TitleFlowIndicator">
<attr name="titlePadding" format="dimension" />
Expand Down
60 changes: 41 additions & 19 deletions viewflow/src/org/taptwo/android/widget/CircleFlowIndicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
* </ul>
*/
public class CircleFlowIndicator extends View implements FlowIndicator {
private static final int INACTIVE_STROKE = 0;
private static final int INACTIVE_FILL = 1;
private static final int STYLE_STROKE = 0;
private static final int STYLE_FILL = 1;
private float radius = 4;
private final Paint mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mPaintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG);
private ViewFlow viewFlow;
private int currentScroll = 0;
private int flowWidth = 0;
Expand All @@ -57,7 +57,7 @@ public class CircleFlowIndicator extends View implements FlowIndicator {
*/
public CircleFlowIndicator(Context context) {
super(context);
initColors(0xFFFFFFFF, 0xFFFFFFFF, INACTIVE_STROKE);
initColors(0xFFFFFFFF, 0xFFFFFFFF, STYLE_FILL, STYLE_STROKE);
}

/**
Expand All @@ -71,44 +71,66 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
// Retrieve styles attributs
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CircleFlowIndicator);
// Retrieve the colors to be used for this view and apply them.
int activeColor = a.getColor(R.styleable.CircleFlowIndicator_fillColor,
0xFFFFFFFF);

// Gets the inactive circle type, defaulting to "fill"
int activeType = a.getInt(
R.styleable.CircleFlowIndicator_activeType, STYLE_FILL);
// Work out the active color based on the type
int activeDefaultColor = 0xFFFFFFFF;
switch (activeType) {
case STYLE_STROKE:
activeDefaultColor = 0xFFFFC445;
break;
case STYLE_FILL:
activeDefaultColor = 0xFFFFFFFF;
}
// Get a custom inactive color if there is one
int activeColor = a.getColor(
R.styleable.CircleFlowIndicator_activeColor,
activeDefaultColor);

// Gets the inactive circle type, defaulting to "stroke"
int inactiveType = a.getInt(
R.styleable.CircleFlowIndicator_inactiveType, INACTIVE_STROKE);
R.styleable.CircleFlowIndicator_inactiveType, STYLE_STROKE);
// Work out the inactive color based on the type
int inactiveDefaultColor = 0xFFFFFFFF;
switch (inactiveType) {
case INACTIVE_STROKE:
case STYLE_STROKE:
inactiveDefaultColor = 0xFFFFFFFF;
break;
case INACTIVE_FILL:
case STYLE_FILL:
inactiveDefaultColor = 0x44FFFFFF;
}
// Get a custom inactive color if there is one
int inactiveColor = a.getColor(
R.styleable.CircleFlowIndicator_strokeColor,
R.styleable.CircleFlowIndicator_inactiveColor,
inactiveDefaultColor);

// Retrieve the radius
radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 4.0f);
initColors(activeColor, inactiveColor, inactiveType);
initColors(activeColor, inactiveColor, activeType, inactiveType);
}

private void initColors(int activeColor, int inactiveColor, int inactiveType) {
private void initColors(int activeColor, int inactiveColor, int activeType, int inactiveType) {
// Select the paint type given the type attr
switch (inactiveType) {
case INACTIVE_STROKE:
case STYLE_STROKE:
mPaintStroke.setStyle(Style.STROKE);
break;
case INACTIVE_FILL:
case STYLE_FILL:
mPaintStroke.setStyle(Style.FILL);
}
mPaintStroke.setColor(inactiveColor);

mPaintFill.setStyle(Style.FILL);
mPaintFill.setColor(activeColor);
// Select the paint type given the type attr
switch (activeType) {
case STYLE_STROKE:
mPaintActive.setStyle(Style.STROKE);
break;
case STYLE_FILL:
mPaintActive.setStyle(Style.FILL);
}
mPaintActive.setColor(activeColor);
}

/*
Expand Down Expand Up @@ -136,7 +158,7 @@ protected void onDraw(Canvas canvas) {
}
// The flow width has been upadated yet. Draw the default position
canvas.drawCircle(getPaddingLeft() + radius + cx, getPaddingTop()
+ radius, radius, mPaintFill);
+ radius, radius, mPaintActive);

}

Expand Down Expand Up @@ -257,7 +279,7 @@ private int measureHeight(int measureSpec) {
* ARGB value for the text
*/
public void setFillColor(int color) {
mPaintFill.setColor(color);
mPaintActive.setColor(color);
invalidate();
}

Expand Down

0 comments on commit fc68dd1

Please sign in to comment.