| 23410 |
tejbeer |
1 |
/*
|
|
|
2 |
* Copyright (C) 2011 Patrik Akerfeldt
|
|
|
3 |
* Copyright (C) 2011 Jake Wharton
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
6 |
* you may not use this file except in compliance with the License.
|
|
|
7 |
* You may obtain a copy of the License at
|
|
|
8 |
*
|
|
|
9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
10 |
*
|
|
|
11 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
14 |
* See the License for the specific language governing permissions and
|
|
|
15 |
* limitations under the License.
|
|
|
16 |
*/
|
|
|
17 |
package com.viewpagerindicator;
|
|
|
18 |
|
|
|
19 |
import android.content.Context;
|
|
|
20 |
import android.content.res.Resources;
|
|
|
21 |
import android.content.res.TypedArray;
|
|
|
22 |
import android.graphics.Canvas;
|
|
|
23 |
import android.graphics.Paint;
|
|
|
24 |
import android.graphics.Paint.Style;
|
|
|
25 |
import android.graphics.drawable.Drawable;
|
|
|
26 |
import android.os.Parcel;
|
|
|
27 |
import android.os.Parcelable;
|
|
|
28 |
import android.support.v4.view.MotionEventCompat;
|
|
|
29 |
import android.support.v4.view.ViewConfigurationCompat;
|
|
|
30 |
import android.support.v4.view.ViewPager;
|
|
|
31 |
import android.util.AttributeSet;
|
|
|
32 |
import android.view.MotionEvent;
|
|
|
33 |
import android.view.View;
|
|
|
34 |
import android.view.ViewConfiguration;
|
|
|
35 |
|
|
|
36 |
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
|
|
|
37 |
import static android.widget.LinearLayout.HORIZONTAL;
|
|
|
38 |
import static android.widget.LinearLayout.VERTICAL;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Draws circles (one for each view). The current view position is filled and
|
|
|
42 |
* others are only stroked.
|
|
|
43 |
*/
|
|
|
44 |
public class CirclePageIndicator extends View implements PageIndicator {
|
|
|
45 |
private static final int INVALID_POINTER = -1;
|
|
|
46 |
|
|
|
47 |
private float mRadius;
|
|
|
48 |
private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
|
|
|
49 |
private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
|
|
|
50 |
private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
|
|
|
51 |
private ViewPager mViewPager;
|
|
|
52 |
private ViewPager.OnPageChangeListener mListener;
|
|
|
53 |
private int mCurrentPage;
|
|
|
54 |
private int mSnapPage;
|
|
|
55 |
private float mPageOffset;
|
|
|
56 |
private int mScrollState;
|
|
|
57 |
private int mOrientation;
|
|
|
58 |
private boolean mCentered;
|
|
|
59 |
private boolean mSnap;
|
|
|
60 |
|
|
|
61 |
private int mTouchSlop;
|
|
|
62 |
private float mLastMotionX = -1;
|
|
|
63 |
private int mActivePointerId = INVALID_POINTER;
|
|
|
64 |
private boolean mIsDragging;
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
public CirclePageIndicator(Context context) {
|
|
|
68 |
this(context, null);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public CirclePageIndicator(Context context, AttributeSet attrs) {
|
|
|
72 |
this(context, attrs, R.attr.vpiCirclePageIndicatorStyle);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) {
|
|
|
76 |
super(context, attrs, defStyle);
|
|
|
77 |
if (isInEditMode()) return;
|
|
|
78 |
|
|
|
79 |
//Load defaults from resources
|
|
|
80 |
final Resources res = getResources();
|
|
|
81 |
final int defaultPageColor = res.getColor(R.color.default_circle_indicator_page_color);
|
|
|
82 |
final int defaultFillColor = res.getColor(R.color.default_circle_indicator_fill_color);
|
|
|
83 |
final int defaultOrientation = res.getInteger(R.integer.default_circle_indicator_orientation);
|
|
|
84 |
final int defaultStrokeColor = res.getColor(R.color.default_circle_indicator_stroke_color);
|
|
|
85 |
final float defaultStrokeWidth = res.getDimension(R.dimen.default_circle_indicator_stroke_width);
|
|
|
86 |
final float defaultRadius = res.getDimension(R.dimen.default_circle_indicator_radius);
|
|
|
87 |
final boolean defaultCentered = res.getBoolean(R.bool.default_circle_indicator_centered);
|
|
|
88 |
final boolean defaultSnap = res.getBoolean(R.bool.default_circle_indicator_snap);
|
|
|
89 |
|
|
|
90 |
//Retrieve styles attributes
|
|
|
91 |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyle, 0);
|
|
|
92 |
|
|
|
93 |
mCentered = a.getBoolean(R.styleable.CirclePageIndicator_centered, defaultCentered);
|
|
|
94 |
mOrientation = a.getInt(R.styleable.CirclePageIndicator_android_orientation, defaultOrientation);
|
|
|
95 |
mPaintPageFill.setStyle(Style.FILL);
|
|
|
96 |
mPaintPageFill.setColor(a.getColor(R.styleable.CirclePageIndicator_pageColor, defaultPageColor));
|
|
|
97 |
mPaintStroke.setStyle(Style.STROKE);
|
|
|
98 |
mPaintStroke.setColor(a.getColor(R.styleable.CirclePageIndicator_strokeColor, defaultStrokeColor));
|
|
|
99 |
mPaintStroke.setStrokeWidth(a.getDimension(R.styleable.CirclePageIndicator_strokeWidth, defaultStrokeWidth));
|
|
|
100 |
mPaintFill.setStyle(Style.FILL);
|
|
|
101 |
mPaintFill.setColor(a.getColor(R.styleable.CirclePageIndicator_fillColor, defaultFillColor));
|
|
|
102 |
mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius, defaultRadius);
|
|
|
103 |
mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap);
|
|
|
104 |
|
|
|
105 |
Drawable background = a.getDrawable(R.styleable.CirclePageIndicator_android_background);
|
|
|
106 |
if (background != null) {
|
|
|
107 |
setBackgroundDrawable(background);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
a.recycle();
|
|
|
111 |
|
|
|
112 |
final ViewConfiguration configuration = ViewConfiguration.get(context);
|
|
|
113 |
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
public void setCentered(boolean centered) {
|
|
|
118 |
mCentered = centered;
|
|
|
119 |
invalidate();
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
public boolean isCentered() {
|
|
|
123 |
return mCentered;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public void setPageColor(int pageColor) {
|
|
|
127 |
mPaintPageFill.setColor(pageColor);
|
|
|
128 |
invalidate();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public int getPageColor() {
|
|
|
132 |
return mPaintPageFill.getColor();
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public void setFillColor(int fillColor) {
|
|
|
136 |
mPaintFill.setColor(fillColor);
|
|
|
137 |
invalidate();
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public int getFillColor() {
|
|
|
141 |
return mPaintFill.getColor();
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public void setOrientation(int orientation) {
|
|
|
145 |
switch (orientation) {
|
|
|
146 |
case HORIZONTAL:
|
|
|
147 |
case VERTICAL:
|
|
|
148 |
mOrientation = orientation;
|
|
|
149 |
requestLayout();
|
|
|
150 |
break;
|
|
|
151 |
|
|
|
152 |
default:
|
|
|
153 |
throw new IllegalArgumentException("Orientation must be either HORIZONTAL or VERTICAL.");
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public int getOrientation() {
|
|
|
158 |
return mOrientation;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public void setStrokeColor(int strokeColor) {
|
|
|
162 |
mPaintStroke.setColor(strokeColor);
|
|
|
163 |
invalidate();
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public int getStrokeColor() {
|
|
|
167 |
return mPaintStroke.getColor();
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public void setStrokeWidth(float strokeWidth) {
|
|
|
171 |
mPaintStroke.setStrokeWidth(strokeWidth);
|
|
|
172 |
invalidate();
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
public float getStrokeWidth() {
|
|
|
176 |
return mPaintStroke.getStrokeWidth();
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public void setRadius(float radius) {
|
|
|
180 |
mRadius = radius;
|
|
|
181 |
invalidate();
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
public float getRadius() {
|
|
|
185 |
return mRadius;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
public void setSnap(boolean snap) {
|
|
|
189 |
mSnap = snap;
|
|
|
190 |
invalidate();
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public boolean isSnap() {
|
|
|
194 |
return mSnap;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
@Override
|
|
|
198 |
protected void onDraw(Canvas canvas) {
|
|
|
199 |
super.onDraw(canvas);
|
|
|
200 |
|
|
|
201 |
if (mViewPager == null) {
|
|
|
202 |
return;
|
|
|
203 |
}
|
|
|
204 |
final int count = mViewPager.getAdapter().getCount();
|
|
|
205 |
if (count == 0) {
|
|
|
206 |
return;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
if (mCurrentPage >= count) {
|
|
|
210 |
setCurrentItem(count - 1);
|
|
|
211 |
return;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
int longSize;
|
|
|
215 |
int longPaddingBefore;
|
|
|
216 |
int longPaddingAfter;
|
|
|
217 |
int shortPaddingBefore;
|
|
|
218 |
if (mOrientation == HORIZONTAL) {
|
|
|
219 |
longSize = getWidth();
|
|
|
220 |
longPaddingBefore = getPaddingLeft();
|
|
|
221 |
longPaddingAfter = getPaddingRight();
|
|
|
222 |
shortPaddingBefore = getPaddingTop();
|
|
|
223 |
} else {
|
|
|
224 |
longSize = getHeight();
|
|
|
225 |
longPaddingBefore = getPaddingTop();
|
|
|
226 |
longPaddingAfter = getPaddingBottom();
|
|
|
227 |
shortPaddingBefore = getPaddingLeft();
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
final float threeRadius = mRadius * 3;
|
|
|
231 |
final float shortOffset = shortPaddingBefore + mRadius;
|
|
|
232 |
float longOffset = longPaddingBefore + mRadius;
|
|
|
233 |
if (mCentered) {
|
|
|
234 |
longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
float dX;
|
|
|
238 |
float dY;
|
|
|
239 |
|
|
|
240 |
float pageFillRadius = mRadius;
|
|
|
241 |
if (mPaintStroke.getStrokeWidth() > 0) {
|
|
|
242 |
pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
//Draw stroked circles
|
|
|
246 |
for (int iLoop = 0; iLoop < count; iLoop++) {
|
|
|
247 |
float drawLong = longOffset + (iLoop * threeRadius);
|
|
|
248 |
if (mOrientation == HORIZONTAL) {
|
|
|
249 |
dX = drawLong;
|
|
|
250 |
dY = shortOffset;
|
|
|
251 |
} else {
|
|
|
252 |
dX = shortOffset;
|
|
|
253 |
dY = drawLong;
|
|
|
254 |
}
|
|
|
255 |
// Only paint fill if not completely transparent
|
|
|
256 |
if (mPaintPageFill.getAlpha() > 0) {
|
|
|
257 |
canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
// Only paint stroke if a stroke width was non-zero
|
|
|
261 |
if (pageFillRadius != mRadius) {
|
|
|
262 |
canvas.drawCircle(dX, dY, mRadius, mPaintStroke);
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
//Draw the filled circle according to the current scroll
|
|
|
267 |
float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
|
|
|
268 |
if (!mSnap) {
|
|
|
269 |
cx += mPageOffset * threeRadius;
|
|
|
270 |
}
|
|
|
271 |
if (mOrientation == HORIZONTAL) {
|
|
|
272 |
dX = longOffset + cx;
|
|
|
273 |
dY = shortOffset;
|
|
|
274 |
} else {
|
|
|
275 |
dX = shortOffset;
|
|
|
276 |
dY = longOffset + cx;
|
|
|
277 |
}
|
|
|
278 |
canvas.drawCircle(dX, dY, mRadius, mPaintFill);
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public boolean onTouchEvent(android.view.MotionEvent ev) {
|
|
|
282 |
if (super.onTouchEvent(ev)) {
|
|
|
283 |
return true;
|
|
|
284 |
}
|
|
|
285 |
if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
|
|
|
286 |
return false;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
|
|
|
290 |
switch (action) {
|
|
|
291 |
case MotionEvent.ACTION_DOWN:
|
|
|
292 |
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
|
|
|
293 |
mLastMotionX = ev.getX();
|
|
|
294 |
break;
|
|
|
295 |
|
|
|
296 |
case MotionEvent.ACTION_MOVE: {
|
|
|
297 |
final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
|
|
|
298 |
final float x = MotionEventCompat.getX(ev, activePointerIndex);
|
|
|
299 |
final float deltaX = x - mLastMotionX;
|
|
|
300 |
|
|
|
301 |
if (!mIsDragging) {
|
|
|
302 |
if (Math.abs(deltaX) > mTouchSlop) {
|
|
|
303 |
mIsDragging = true;
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if (mIsDragging) {
|
|
|
308 |
mLastMotionX = x;
|
|
|
309 |
if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
|
|
|
310 |
mViewPager.fakeDragBy(deltaX);
|
|
|
311 |
}
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
break;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
case MotionEvent.ACTION_CANCEL:
|
|
|
318 |
case MotionEvent.ACTION_UP:
|
|
|
319 |
if (!mIsDragging) {
|
|
|
320 |
final int count = mViewPager.getAdapter().getCount();
|
|
|
321 |
final int width = getWidth();
|
|
|
322 |
final float halfWidth = width / 2f;
|
|
|
323 |
final float sixthWidth = width / 6f;
|
|
|
324 |
|
|
|
325 |
if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
|
|
|
326 |
if (action != MotionEvent.ACTION_CANCEL) {
|
|
|
327 |
mViewPager.setCurrentItem(mCurrentPage - 1);
|
|
|
328 |
}
|
|
|
329 |
return true;
|
|
|
330 |
} else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
|
|
|
331 |
if (action != MotionEvent.ACTION_CANCEL) {
|
|
|
332 |
mViewPager.setCurrentItem(mCurrentPage + 1);
|
|
|
333 |
}
|
|
|
334 |
return true;
|
|
|
335 |
}
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
mIsDragging = false;
|
|
|
339 |
mActivePointerId = INVALID_POINTER;
|
|
|
340 |
if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
|
|
|
341 |
break;
|
|
|
342 |
|
|
|
343 |
case MotionEventCompat.ACTION_POINTER_DOWN: {
|
|
|
344 |
final int index = MotionEventCompat.getActionIndex(ev);
|
|
|
345 |
mLastMotionX = MotionEventCompat.getX(ev, index);
|
|
|
346 |
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
|
|
|
347 |
break;
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
case MotionEventCompat.ACTION_POINTER_UP:
|
|
|
351 |
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
|
|
|
352 |
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
|
|
|
353 |
if (pointerId == mActivePointerId) {
|
|
|
354 |
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
|
|
|
355 |
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
|
|
|
356 |
}
|
|
|
357 |
mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
|
|
|
358 |
break;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
return true;
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
@Override
|
|
|
365 |
public void setViewPager(ViewPager view) {
|
|
|
366 |
if (mViewPager == view) {
|
|
|
367 |
return;
|
|
|
368 |
}
|
|
|
369 |
if (mViewPager != null) {
|
|
|
370 |
mViewPager.setOnPageChangeListener(null);
|
|
|
371 |
}
|
|
|
372 |
if (view.getAdapter() == null) {
|
|
|
373 |
throw new IllegalStateException("ViewPager does not have adapter instance.");
|
|
|
374 |
}
|
|
|
375 |
mViewPager = view;
|
|
|
376 |
mViewPager.setOnPageChangeListener(this);
|
|
|
377 |
invalidate();
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
@Override
|
|
|
381 |
public void setViewPager(ViewPager view, int initialPosition) {
|
|
|
382 |
setViewPager(view);
|
|
|
383 |
setCurrentItem(initialPosition);
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
@Override
|
|
|
387 |
public void setCurrentItem(int item) {
|
|
|
388 |
if (mViewPager == null) {
|
|
|
389 |
throw new IllegalStateException("ViewPager has not been bound.");
|
|
|
390 |
}
|
|
|
391 |
mViewPager.setCurrentItem(item);
|
|
|
392 |
mCurrentPage = item;
|
|
|
393 |
invalidate();
|
|
|
394 |
}
|
|
|
395 |
|
|
|
396 |
@Override
|
|
|
397 |
public void notifyDataSetChanged() {
|
|
|
398 |
invalidate();
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
@Override
|
|
|
402 |
public void onPageScrollStateChanged(int state) {
|
|
|
403 |
mScrollState = state;
|
|
|
404 |
|
|
|
405 |
if (mListener != null) {
|
|
|
406 |
mListener.onPageScrollStateChanged(state);
|
|
|
407 |
}
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
@Override
|
|
|
411 |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
412 |
mCurrentPage = position;
|
|
|
413 |
mPageOffset = positionOffset;
|
|
|
414 |
invalidate();
|
|
|
415 |
|
|
|
416 |
if (mListener != null) {
|
|
|
417 |
mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
|
|
|
418 |
}
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
@Override
|
|
|
422 |
public void onPageSelected(int position) {
|
|
|
423 |
if (mSnap || mScrollState == ViewPager.SCROLL_STATE_IDLE) {
|
|
|
424 |
mCurrentPage = position;
|
|
|
425 |
mSnapPage = position;
|
|
|
426 |
invalidate();
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
if (mListener != null) {
|
|
|
430 |
mListener.onPageSelected(position);
|
|
|
431 |
}
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
@Override
|
|
|
435 |
public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
|
|
|
436 |
mListener = listener;
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
/*
|
|
|
440 |
* (non-Javadoc)
|
|
|
441 |
*
|
|
|
442 |
* @see android.view.View#onMeasure(int, int)
|
|
|
443 |
*/
|
|
|
444 |
@Override
|
|
|
445 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
446 |
if (mOrientation == HORIZONTAL) {
|
|
|
447 |
setMeasuredDimension(measureLong(widthMeasureSpec), measureShort(heightMeasureSpec));
|
|
|
448 |
} else {
|
|
|
449 |
setMeasuredDimension(measureShort(widthMeasureSpec), measureLong(heightMeasureSpec));
|
|
|
450 |
}
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
/**
|
|
|
454 |
* Determines the width of this view
|
|
|
455 |
*
|
|
|
456 |
* @param measureSpec
|
|
|
457 |
* A measureSpec packed into an int
|
|
|
458 |
* @return The width of the view, honoring constraints from measureSpec
|
|
|
459 |
*/
|
|
|
460 |
private int measureLong(int measureSpec) {
|
|
|
461 |
int result;
|
|
|
462 |
int specMode = MeasureSpec.getMode(measureSpec);
|
|
|
463 |
int specSize = MeasureSpec.getSize(measureSpec);
|
|
|
464 |
|
|
|
465 |
if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
|
|
|
466 |
//We were told how big to be
|
|
|
467 |
result = specSize;
|
|
|
468 |
} else {
|
|
|
469 |
//Calculate the width according the views count
|
|
|
470 |
final int count = mViewPager.getAdapter().getCount();
|
|
|
471 |
result = (int)(getPaddingLeft() + getPaddingRight()
|
|
|
472 |
+ (count * 2 * mRadius) + (count - 1) * mRadius + 1);
|
|
|
473 |
//Respect AT_MOST value if that was what is called for by measureSpec
|
|
|
474 |
if (specMode == MeasureSpec.AT_MOST) {
|
|
|
475 |
result = Math.min(result, specSize);
|
|
|
476 |
}
|
|
|
477 |
}
|
|
|
478 |
return result;
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
/**
|
|
|
482 |
* Determines the height of this view
|
|
|
483 |
*
|
|
|
484 |
* @param measureSpec
|
|
|
485 |
* A measureSpec packed into an int
|
|
|
486 |
* @return The height of the view, honoring constraints from measureSpec
|
|
|
487 |
*/
|
|
|
488 |
private int measureShort(int measureSpec) {
|
|
|
489 |
int result;
|
|
|
490 |
int specMode = MeasureSpec.getMode(measureSpec);
|
|
|
491 |
int specSize = MeasureSpec.getSize(measureSpec);
|
|
|
492 |
|
|
|
493 |
if (specMode == MeasureSpec.EXACTLY) {
|
|
|
494 |
//We were told how big to be
|
|
|
495 |
result = specSize;
|
|
|
496 |
} else {
|
|
|
497 |
//Measure the height
|
|
|
498 |
result = (int)(2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
|
|
|
499 |
//Respect AT_MOST value if that was what is called for by measureSpec
|
|
|
500 |
if (specMode == MeasureSpec.AT_MOST) {
|
|
|
501 |
result = Math.min(result, specSize);
|
|
|
502 |
}
|
|
|
503 |
}
|
|
|
504 |
return result;
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
@Override
|
|
|
508 |
public void onRestoreInstanceState(Parcelable state) {
|
|
|
509 |
SavedState savedState = (SavedState)state;
|
|
|
510 |
super.onRestoreInstanceState(savedState.getSuperState());
|
|
|
511 |
mCurrentPage = savedState.currentPage;
|
|
|
512 |
mSnapPage = savedState.currentPage;
|
|
|
513 |
requestLayout();
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
@Override
|
|
|
517 |
public Parcelable onSaveInstanceState() {
|
|
|
518 |
Parcelable superState = super.onSaveInstanceState();
|
|
|
519 |
SavedState savedState = new SavedState(superState);
|
|
|
520 |
savedState.currentPage = mCurrentPage;
|
|
|
521 |
return savedState;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
static class SavedState extends BaseSavedState {
|
|
|
525 |
int currentPage;
|
|
|
526 |
|
|
|
527 |
public SavedState(Parcelable superState) {
|
|
|
528 |
super(superState);
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
private SavedState(Parcel in) {
|
|
|
532 |
super(in);
|
|
|
533 |
currentPage = in.readInt();
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
@Override
|
|
|
537 |
public void writeToParcel(Parcel dest, int flags) {
|
|
|
538 |
super.writeToParcel(dest, flags);
|
|
|
539 |
dest.writeInt(currentPage);
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
@SuppressWarnings("UnusedDeclaration")
|
|
|
543 |
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
|
|
|
544 |
@Override
|
|
|
545 |
public SavedState createFromParcel(Parcel in) {
|
|
|
546 |
return new SavedState(in);
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
@Override
|
|
|
550 |
public SavedState[] newArray(int size) {
|
|
|
551 |
return new SavedState[size];
|
|
|
552 |
}
|
|
|
553 |
};
|
|
|
554 |
}
|
|
|
555 |
}
|