Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

Add spacing attribute to CirclePageIndicator #367

Open
wants to merge 2 commits 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
4 changes: 3 additions & 1 deletion library/res/values/vpi__attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
<attr name="pageColor" format="color" />
<!-- Orientation of the indicator. -->
<attr name="android:orientation"/>
<!-- Radius of the circles. This is also the spacing between circles. -->
<!-- Radius of the circles. -->
<attr name="radius" format="dimension" />
<!-- Spacing between two circles. -->
<attr name="spacing" format="dimension" />
<!-- Whether or not the selected indicator snaps to the circles. -->
<attr name="snap" format="boolean" />
<!-- Color of the open circles. -->
Expand Down
21 changes: 16 additions & 5 deletions library/src/com/viewpagerindicator/CirclePageIndicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CirclePageIndicator extends View implements PageIndicator {
private static final int INVALID_POINTER = -1;

private float mRadius;
private float mSpacing;
private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
Expand Down Expand Up @@ -100,6 +101,7 @@ public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
mPaintFill.setStyle(Style.FILL);
mPaintFill.setColor(a.getColor(R.styleable.CirclePageIndicator_fillColor, defaultFillColor));
mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius, defaultRadius);
mSpacing = a.getDimension(R.styleable.CirclePageIndicator_spacing, mRadius);
mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap);

Drawable background = a.getDrawable(R.styleable.CirclePageIndicator_android_background);
Expand Down Expand Up @@ -185,6 +187,15 @@ public float getRadius() {
return mRadius;
}

public void setSpacing(float spacing) {
mSpacing = spacing;
invalidate();
}

public float getSpacing() {
return mSpacing;
}

public void setSnap(boolean snap) {
mSnap = snap;
invalidate();
Expand Down Expand Up @@ -227,11 +238,11 @@ protected void onDraw(Canvas canvas) {
shortPaddingBefore = getPaddingLeft();
}

final float threeRadius = mRadius * 3;
final float offsetDiff = mRadius * 2 + mSpacing;
final float shortOffset = shortPaddingBefore + mRadius;
float longOffset = longPaddingBefore + mRadius;
if (mCentered) {
longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - (((count - 1) * offsetDiff + mRadius * 2) / 2.0f);
}

float dX;
Expand All @@ -244,7 +255,7 @@ protected void onDraw(Canvas canvas) {

//Draw stroked circles
for (int iLoop = 0; iLoop < count; iLoop++) {
float drawLong = longOffset + (iLoop * threeRadius);
float drawLong = longOffset + (iLoop * offsetDiff);
if (mOrientation == HORIZONTAL) {
dX = drawLong;
dY = shortOffset;
Expand All @@ -264,9 +275,9 @@ protected void onDraw(Canvas canvas) {
}

//Draw the filled circle according to the current scroll
float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
float cx = (mSnap ? mSnapPage : mCurrentPage) * offsetDiff;
if (!mSnap) {
cx += mPageOffset * threeRadius;
cx += mPageOffset * offsetDiff;
}
if (mOrientation == HORIZONTAL) {
dX = longOffset + cx;
Expand Down