Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.

Commit f7d4980

Browse files
committed
Add snap option to CircleFlowIndicator
like ViewPagerIndicator's. It makes the active indicator jump to the next position instead of following the scroll offsets. I've also documentated it in the README.
1 parent 5d84180 commit f7d4980

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ And then you'll need to connect your `ViewFlow` with the `FlowIndicator`:
6666

6767
CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
6868
viewFlow.setFlowIndicator(indic);
69-
70-
The following attributes are supported: `activeColor`, `inactiveColor`, `activeType` (either fill or stroke), `inactiveType` (either fill or stroke), `fadeOut` (time in ms until indicator fades out, 0 = never), `radius`.
69+
70+
By default, the 'active' indicator moves smoothly from one 'inactive' indicator
71+
to the next, as the user scrolls. If you set the `snap` attribute to `true`, it
72+
will instead jump to the next position when the flow settles at the next page.
73+
74+
The following attributes are supported: `activeColor`, `inactiveColor`,
75+
`activeType` (either fill or stroke), `inactiveType` (either fill or stroke),
76+
`fadeOut` (time in ms until indicator fades out, 0 = never), `radius`, `sync`
77+
(see above).
7178

7279
#### Title Flow Indicator ####
7380
This indicator presents the title of the previous, current and next `View` in the adapter (see screenshot below).

viewflow/res/values/attrs.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<flag name="stroke" value="0" />
3434
<flag name="fill" value="1" />
3535
</attr>
36+
<attr name="snap" format="boolean" />
3637
</declare-styleable>
3738
<declare-styleable name="TitleFlowIndicator">
3839
<attr name="titlePadding" format="dimension" />
@@ -48,4 +49,4 @@
4849
<attr name="footerTriangleHeight" format="dimension" />
4950
<attr name="customTypeface" format="string" />
5051
</declare-styleable>
51-
</resources>
52+
</resources>

viewflow/src/org/taptwo/android/widget/CircleFlowIndicator.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
* <li>
5555
* spacing: Define the circle spacing (default to 4.0)
5656
* </li>
57+
* <li>
58+
* snap: If true, the 'active' indicator snaps from one page to the next; otherwise, it moves smoothly.
59+
* </li>
5760
* </ul>
5861
*/
5962
public class CircleFlowIndicator extends View implements FlowIndicator,
@@ -70,11 +73,13 @@ public class CircleFlowIndicator extends View implements FlowIndicator,
7073
private final Paint mPaintActive = new Paint(Paint.ANTI_ALIAS_FLAG);
7174
private ViewFlow viewFlow;
7275
private int currentScroll = 0;
76+
private int currentPosition = 0;
7377
private int flowWidth = 0;
7478
private FadeTimer timer;
7579
public AnimationListener animationListener = this;
7680
private Animation animation;
7781
private boolean mCentered = false;
82+
private boolean mSnap = false;
7883

7984
/**
8085
* Default constructor
@@ -133,6 +138,8 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
133138
fadeOutTime = a.getInt(R.styleable.CircleFlowIndicator_fadeOut, 0);
134139

135140
mCentered = a.getBoolean(R.styleable.CircleFlowIndicator_centered, false);
141+
142+
mSnap = a.getBoolean(R.styleable.CircleFlowIndicator_snap, false);
136143

137144
initColors(activeColor, inactiveColor, activeType, inactiveType);
138145
}
@@ -197,11 +204,15 @@ protected void onDraw(Canvas canvas) {
197204
getPaddingTop() + mRadius, mRadiusInactive, mPaintInactive);
198205
}
199206
float cx = 0;
200-
if (flowWidth != 0) {
201-
// Draw the filled circle according to the current scroll
202-
cx = (currentScroll * spacing) / flowWidth;
207+
if (mSnap) {
208+
cx = currentPosition * spacing;
209+
} else {
210+
if (flowWidth != 0) {
211+
// Draw the filled circle according to the current scroll
212+
cx = (currentScroll * spacing) / flowWidth;
213+
}
214+
// else, the flow width hasn't been updated yet. Draw the default position.
203215
}
204-
// The flow width has been upadated yet. Draw the default position
205216
canvas.drawCircle(leftPadding + mRadius + cx+centeringOffset, getPaddingTop()
206217
+ mRadius, mRadiusActive, mPaintActive);
207218
}
@@ -215,6 +226,12 @@ protected void onDraw(Canvas canvas) {
215226
*/
216227
@Override
217228
public void onSwitched(View view, int position) {
229+
currentPosition = position;
230+
if (mSnap) {
231+
setVisibility(View.VISIBLE);
232+
resetTimer();
233+
invalidate();
234+
}
218235
}
219236

220237
/*
@@ -240,11 +257,13 @@ public void setViewFlow(ViewFlow view) {
240257
*/
241258
@Override
242259
public void onScrolled(int h, int v, int oldh, int oldv) {
243-
setVisibility(View.VISIBLE);
244-
resetTimer();
245260
currentScroll = h;
246261
flowWidth = viewFlow.getChildWidth();
247-
invalidate();
262+
if (!mSnap) {
263+
setVisibility(View.VISIBLE);
264+
resetTimer();
265+
invalidate();
266+
}
248267
}
249268

250269
/*

0 commit comments

Comments
 (0)