5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * http://www.apache.org/licenses/LICENSE-2.0
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
15
15
*/
16
16
package org .taptwo .android .widget ;
17
17
18
- import org .taptwo .android .widget .viewflow .R ;
19
-
20
18
import android .content .Context ;
21
19
import android .content .res .TypedArray ;
22
20
import android .graphics .Canvas ;
26
24
import android .util .AttributeSet ;
27
25
import android .view .View ;
28
26
import android .view .animation .Animation ;
29
- import android .view .animation .AnimationUtils ;
30
27
import android .view .animation .Animation .AnimationListener ;
28
+ import android .view .animation .AnimationUtils ;
29
+ import org .taptwo .android .widget .viewflow .R ;
31
30
32
31
/**
33
32
* A FlowIndicator which draws circles (one for each view).
49
48
* fadeOut: Define the time (in ms) until the indicator will fade out (default to 0 = never fade out)
50
49
* </ul>
51
50
* <ul>
52
- * radius: Define the circle radius (default to 4.0)
51
+ * radius: Define the circle outer radius (default to 4.0)
53
52
* </ul>
54
- * * <ul>
53
+ * * <ul>
55
54
* spacing: Define the circle spacing (default to 4.0)
56
55
* </ul>
57
56
*/
@@ -60,7 +59,9 @@ public class CircleFlowIndicator extends View implements FlowIndicator,
60
59
private static final int STYLE_STROKE = 0 ;
61
60
private static final int STYLE_FILL = 1 ;
62
61
63
- private float radius = 4 ;
62
+ private float mRadius = 4 ;
63
+ private float mRadiusInactive = 4 ;
64
+ private float mRadiusActive = 4 ;
64
65
private float spacing = 4 ;
65
66
private int fadeOutTime = 0 ;
66
67
private final Paint mPaintInactive = new Paint (Paint .ANTI_ALIAS_FLAG );
@@ -117,10 +118,14 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
117
118
inactiveDefaultColor );
118
119
119
120
// Retrieve the radius
120
- radius = a .getDimension (R .styleable .CircleFlowIndicator_radius , 4.0f );
121
-
121
+ mRadius = a .getDimension (R .styleable .CircleFlowIndicator_radius , 4.0f );
122
+ mRadiusActive = mRadius ;
123
+ mRadiusInactive = mRadius ;
124
+
122
125
// Retrieve the spacing
123
126
spacing = a .getDimension (R .styleable .CircleFlowIndicator_spacing , 4.0f );
127
+ // We want the spacing to be center-to-center
128
+ spacing += 2 * mRadiusActive ;
124
129
125
130
// Retrieve the fade out time
126
131
fadeOutTime = a .getInt (R .styleable .CircleFlowIndicator_fadeOut , 0 );
@@ -139,13 +144,25 @@ private void initColors(int activeColor, int inactiveColor, int activeType,
139
144
break ;
140
145
default :
141
146
mPaintInactive .setStyle (Style .STROKE );
147
+ float strokeWidth = mPaintInactive .getStrokeWidth ();
148
+ if (strokeWidth == 0.0f ) {
149
+ // It draws in "hairline mode", which is 1 px wide.
150
+ strokeWidth = 1.0f / getResources ().getDisplayMetrics ().density ;
151
+ }
152
+ mRadiusInactive -= strokeWidth / 2.0f ;
142
153
}
143
154
mPaintInactive .setColor (inactiveColor );
144
155
145
156
// Select the paint type given the type attr
146
157
switch (activeType ) {
147
158
case STYLE_STROKE :
148
159
mPaintActive .setStyle (Style .STROKE );
160
+ float strokeWidth = mPaintInactive .getStrokeWidth ();
161
+ if (strokeWidth == 0.0f ) {
162
+ // It draws in "hairline mode", which is 1 px wide.
163
+ strokeWidth = 1.0f / getResources ().getDisplayMetrics ().density ;
164
+ }
165
+ mRadiusActive -= strokeWidth / 2.0f ;
149
166
break ;
150
167
default :
151
168
mPaintActive .setStyle (Style .FILL );
@@ -166,26 +183,25 @@ protected void onDraw(Canvas canvas) {
166
183
count = viewFlow .getViewsCount ();
167
184
}
168
185
169
- float circleSeparation = 2 *radius +spacing ;
170
186
//this is the amount the first circle should be offset to make the entire thing centered
171
187
float centeringOffset = 0 ;
172
188
173
189
int leftPadding = getPaddingLeft ();
174
190
175
191
// Draw stroked circles
176
192
for (int iLoop = 0 ; iLoop < count ; iLoop ++) {
177
- canvas .drawCircle (leftPadding + radius
178
- + (iLoop * circleSeparation ) + centeringOffset ,
179
- getPaddingTop () + radius , radius , mPaintInactive );
193
+ canvas .drawCircle (leftPadding + mRadius
194
+ + (iLoop * spacing ) + centeringOffset ,
195
+ getPaddingTop () + mRadius , mRadiusInactive , mPaintInactive );
180
196
}
181
197
float cx = 0 ;
182
198
if (flowWidth != 0 ) {
183
199
// Draw the filled circle according to the current scroll
184
- cx = (currentScroll * ( 2 * radius + spacing ) ) / flowWidth ;
200
+ cx = (currentScroll * spacing ) / flowWidth ;
185
201
}
186
202
// The flow width has been upadated yet. Draw the default position
187
- canvas .drawCircle (leftPadding + radius + cx +centeringOffset , getPaddingTop ()
188
- + radius , radius , mPaintActive );
203
+ canvas .drawCircle (leftPadding + mRadius + cx +centeringOffset , getPaddingTop ()
204
+ + mRadius , mRadiusActive , mPaintActive );
189
205
}
190
206
191
207
/*
@@ -210,7 +226,7 @@ public void onSwitched(View view, int position) {
210
226
public void setViewFlow (ViewFlow view ) {
211
227
resetTimer ();
212
228
viewFlow = view ;
213
- flowWidth = viewFlow .getWidth ();
229
+ flowWidth = viewFlow .getChildWidth ();
214
230
invalidate ();
215
231
}
216
232
@@ -225,7 +241,7 @@ public void onScrolled(int h, int v, int oldh, int oldv) {
225
241
setVisibility (View .VISIBLE );
226
242
resetTimer ();
227
243
currentScroll = h ;
228
- flowWidth = viewFlow .getWidth ();
244
+ flowWidth = viewFlow .getChildWidth ();
229
245
invalidate ();
230
246
}
231
247
@@ -244,7 +260,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
244
260
* Determines the width of this view
245
261
*
246
262
* @param measureSpec
247
- * A measureSpec packed into an int
263
+ * A measureSpec packed into an int
248
264
* @return The width of the view, honoring constraints from measureSpec
249
265
*/
250
266
private int measureWidth (int measureSpec ) {
@@ -262,8 +278,9 @@ private int measureWidth(int measureSpec) {
262
278
if (viewFlow != null ) {
263
279
count = viewFlow .getViewsCount ();
264
280
}
281
+ // Remember that spacing is centre-to-centre
265
282
result = (int ) (getPaddingLeft () + getPaddingRight ()
266
- + (count * 2 * radius ) + (count - 1 ) * spacing + 1 );
283
+ + (2 * mRadius ) + (count - 1 ) * spacing );
267
284
// Respect AT_MOST value if that was what is called for by
268
285
// measureSpec
269
286
if (specMode == MeasureSpec .AT_MOST ) {
@@ -277,7 +294,7 @@ private int measureWidth(int measureSpec) {
277
294
* Determines the height of this view
278
295
*
279
296
* @param measureSpec
280
- * A measureSpec packed into an int
297
+ * A measureSpec packed into an int
281
298
* @return The height of the view, honoring constraints from measureSpec
282
299
*/
283
300
private int measureHeight (int measureSpec ) {
@@ -291,7 +308,7 @@ private int measureHeight(int measureSpec) {
291
308
}
292
309
// Measure the height
293
310
else {
294
- result = (int ) (2 * radius + getPaddingTop () + getPaddingBottom () + 1 );
311
+ result = (int ) (2 * mRadius + getPaddingTop () + getPaddingBottom () + 1 );
295
312
// Respect AT_MOST value if that was what is called for by
296
313
// measureSpec
297
314
if (specMode == MeasureSpec .AT_MOST ) {
@@ -305,7 +322,7 @@ private int measureHeight(int measureSpec) {
305
322
* Sets the fill color
306
323
*
307
324
* @param color
308
- * ARGB value for the text
325
+ * ARGB value for the text
309
326
*/
310
327
public void setFillColor (int color ) {
311
328
mPaintActive .setColor (color );
@@ -316,7 +333,7 @@ public void setFillColor(int color) {
316
333
* Sets the stroke color
317
334
*
318
335
* @param color
319
- * ARGB value for the text
336
+ * ARGB value for the text
320
337
*/
321
338
public void setStrokeColor (int color ) {
322
339
mPaintInactive .setColor (color );
@@ -398,4 +415,4 @@ public void onAnimationRepeat(Animation animation) {
398
415
@ Override
399
416
public void onAnimationStart (Animation animation ) {
400
417
}
401
- }
418
+ }
0 commit comments