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

Commit 6a57a72

Browse files
committed
Merge pull request #93 from orac/measure
Implement measure and layout so that non-fixed sizes can be used
2 parents b62f003 + b8c13a7 commit 6a57a72

File tree

2 files changed

+190
-80
lines changed

2 files changed

+190
-80
lines changed

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,8 +15,6 @@
1515
*/
1616
package org.taptwo.android.widget;
1717

18-
import org.taptwo.android.widget.viewflow.R;
19-
2018
import android.content.Context;
2119
import android.content.res.TypedArray;
2220
import android.graphics.Canvas;
@@ -26,8 +24,9 @@
2624
import android.util.AttributeSet;
2725
import android.view.View;
2826
import android.view.animation.Animation;
29-
import android.view.animation.AnimationUtils;
3027
import android.view.animation.Animation.AnimationListener;
28+
import android.view.animation.AnimationUtils;
29+
import org.taptwo.android.widget.viewflow.R;
3130

3231
/**
3332
* A FlowIndicator which draws circles (one for each view).
@@ -49,9 +48,9 @@
4948
* fadeOut: Define the time (in ms) until the indicator will fade out (default to 0 = never fade out)
5049
* </ul>
5150
* <ul>
52-
* radius: Define the circle radius (default to 4.0)
51+
* radius: Define the circle outer radius (default to 4.0)
5352
* </ul>
54-
* * <ul>
53+
* * <ul>
5554
* spacing: Define the circle spacing (default to 4.0)
5655
* </ul>
5756
*/
@@ -60,7 +59,9 @@ public class CircleFlowIndicator extends View implements FlowIndicator,
6059
private static final int STYLE_STROKE = 0;
6160
private static final int STYLE_FILL = 1;
6261

63-
private float radius = 4;
62+
private float mRadius = 4;
63+
private float mRadiusInactive = 4;
64+
private float mRadiusActive = 4;
6465
private float spacing = 4;
6566
private int fadeOutTime = 0;
6667
private final Paint mPaintInactive = new Paint(Paint.ANTI_ALIAS_FLAG);
@@ -117,10 +118,14 @@ public CircleFlowIndicator(Context context, AttributeSet attrs) {
117118
inactiveDefaultColor);
118119

119120
// 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+
122125
// Retrieve the spacing
123126
spacing = a.getDimension(R.styleable.CircleFlowIndicator_spacing, 4.0f);
127+
// We want the spacing to be center-to-center
128+
spacing += 2 * mRadiusActive;
124129

125130
// Retrieve the fade out time
126131
fadeOutTime = a.getInt(R.styleable.CircleFlowIndicator_fadeOut, 0);
@@ -139,13 +144,25 @@ private void initColors(int activeColor, int inactiveColor, int activeType,
139144
break;
140145
default:
141146
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;
142153
}
143154
mPaintInactive.setColor(inactiveColor);
144155

145156
// Select the paint type given the type attr
146157
switch (activeType) {
147158
case STYLE_STROKE:
148159
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;
149166
break;
150167
default:
151168
mPaintActive.setStyle(Style.FILL);
@@ -166,26 +183,25 @@ protected void onDraw(Canvas canvas) {
166183
count = viewFlow.getViewsCount();
167184
}
168185

169-
float circleSeparation = 2*radius+spacing;
170186
//this is the amount the first circle should be offset to make the entire thing centered
171187
float centeringOffset = 0;
172188

173189
int leftPadding = getPaddingLeft();
174190

175191
// Draw stroked circles
176192
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);
180196
}
181197
float cx = 0;
182198
if (flowWidth != 0) {
183199
// Draw the filled circle according to the current scroll
184-
cx = (currentScroll * (2 * radius + spacing)) / flowWidth;
200+
cx = (currentScroll * spacing) / flowWidth;
185201
}
186202
// 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);
189205
}
190206

191207
/*
@@ -210,7 +226,7 @@ public void onSwitched(View view, int position) {
210226
public void setViewFlow(ViewFlow view) {
211227
resetTimer();
212228
viewFlow = view;
213-
flowWidth = viewFlow.getWidth();
229+
flowWidth = viewFlow.getChildWidth();
214230
invalidate();
215231
}
216232

@@ -225,7 +241,7 @@ public void onScrolled(int h, int v, int oldh, int oldv) {
225241
setVisibility(View.VISIBLE);
226242
resetTimer();
227243
currentScroll = h;
228-
flowWidth = viewFlow.getWidth();
244+
flowWidth = viewFlow.getChildWidth();
229245
invalidate();
230246
}
231247

@@ -244,7 +260,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
244260
* Determines the width of this view
245261
*
246262
* @param measureSpec
247-
* A measureSpec packed into an int
263+
* A measureSpec packed into an int
248264
* @return The width of the view, honoring constraints from measureSpec
249265
*/
250266
private int measureWidth(int measureSpec) {
@@ -262,8 +278,9 @@ private int measureWidth(int measureSpec) {
262278
if (viewFlow != null) {
263279
count = viewFlow.getViewsCount();
264280
}
281+
// Remember that spacing is centre-to-centre
265282
result = (int) (getPaddingLeft() + getPaddingRight()
266-
+ (count * 2 * radius) + (count - 1) * spacing + 1);
283+
+ (2 * mRadius) + (count - 1) * spacing);
267284
// Respect AT_MOST value if that was what is called for by
268285
// measureSpec
269286
if (specMode == MeasureSpec.AT_MOST) {
@@ -277,7 +294,7 @@ private int measureWidth(int measureSpec) {
277294
* Determines the height of this view
278295
*
279296
* @param measureSpec
280-
* A measureSpec packed into an int
297+
* A measureSpec packed into an int
281298
* @return The height of the view, honoring constraints from measureSpec
282299
*/
283300
private int measureHeight(int measureSpec) {
@@ -291,7 +308,7 @@ private int measureHeight(int measureSpec) {
291308
}
292309
// Measure the height
293310
else {
294-
result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
311+
result = (int) (2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
295312
// Respect AT_MOST value if that was what is called for by
296313
// measureSpec
297314
if (specMode == MeasureSpec.AT_MOST) {
@@ -305,7 +322,7 @@ private int measureHeight(int measureSpec) {
305322
* Sets the fill color
306323
*
307324
* @param color
308-
* ARGB value for the text
325+
* ARGB value for the text
309326
*/
310327
public void setFillColor(int color) {
311328
mPaintActive.setColor(color);
@@ -316,7 +333,7 @@ public void setFillColor(int color) {
316333
* Sets the stroke color
317334
*
318335
* @param color
319-
* ARGB value for the text
336+
* ARGB value for the text
320337
*/
321338
public void setStrokeColor(int color) {
322339
mPaintInactive.setColor(color);
@@ -398,4 +415,4 @@ public void onAnimationRepeat(Animation animation) {
398415
@Override
399416
public void onAnimationStart(Animation animation) {
400417
}
401-
}
418+
}

0 commit comments

Comments
 (0)