SwipeProgressBar.java revision cbfb6c6ffc411f1f9d302570341993bd9929ee1a
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.widget;
18
19import android.graphics.Canvas;
20import android.graphics.Paint;
21import android.graphics.Rect;
22import android.graphics.RectF;
23import android.support.v4.view.ViewCompat;
24import android.view.View;
25import android.view.animation.AnimationUtils;
26import android.view.animation.Interpolator;
27
28
29/**
30 * Custom progress bar that shows a cycle of colors as widening circles that
31 * overdraw each other. When finished, the bar is cleared from the inside out as
32 * the main cycle continues. Before running, this can also indicate how close
33 * the user is to triggering something (e.g. how far they need to pull down to
34 * trigger a refresh).
35 */
36final class SwipeProgressBar {
37
38    // Default progress animation colors are grays.
39    private final static int COLOR1 = 0xB3000000;
40    private final static int COLOR2 = 0x80000000;
41    private final static int COLOR3 = 0x4d000000;
42    private final static int COLOR4 = 0x1a000000;
43
44    // The duration of the animation cycle.
45    private static final int ANIMATION_DURATION_MS = 2000;
46
47    // The duration of the animation to clear the bar.
48    private static final int FINISH_ANIMATION_DURATION_MS = 1000;
49
50    // Interpolator for varying the speed of the animation.
51    private static final Interpolator INTERPOLATOR = BakedBezierInterpolator.getInstance();
52
53    private final Paint mPaint = new Paint();
54    private final RectF mClipRect = new RectF();
55    private float mTriggerPercentage;
56    private long mStartTime;
57    private long mFinishTime;
58    private boolean mRunning;
59
60    // Colors used when rendering the animation,
61    private int mColor1;
62    private int mColor2;
63    private int mColor3;
64    private int mColor4;
65    private View mParent;
66
67    private Rect mBounds = new Rect();
68
69    public SwipeProgressBar(View parent) {
70        mParent = parent;
71        mColor1 = COLOR1;
72        mColor2 = COLOR2;
73        mColor3 = COLOR3;
74        mColor4 = COLOR4;
75    }
76
77    /**
78     * Set the four colors used in the progress animation. The first color will
79     * also be the color of the bar that grows in response to a user swipe
80     * gesture.
81     *
82     * @param color1 Integer representation of a color.
83     * @param color2 Integer representation of a color.
84     * @param color3 Integer representation of a color.
85     * @param color4 Integer representation of a color.
86     */
87    void setColorScheme(int color1, int color2, int color3, int color4) {
88        mColor1 = color1;
89        mColor2 = color2;
90        mColor3 = color3;
91        mColor4 = color4;
92    }
93
94    /**
95     * Update the progress the user has made toward triggering the swipe
96     * gesture. and use this value to update the percentage of the trigger that
97     * is shown.
98     */
99    void setTriggerPercentage(float triggerPercentage) {
100        mTriggerPercentage = triggerPercentage;
101        mStartTime = 0;
102        ViewCompat.postInvalidateOnAnimation(
103                mParent, mBounds.left, mBounds.top, mBounds.right, mBounds.bottom);
104    }
105
106    /**
107     * Start showing the progress animation.
108     */
109    void start() {
110        if (!mRunning) {
111            mTriggerPercentage = 0;
112            mStartTime = AnimationUtils.currentAnimationTimeMillis();
113            mRunning = true;
114            mParent.postInvalidate();
115        }
116    }
117
118    /**
119     * Stop showing the progress animation.
120     */
121    void stop() {
122        if (mRunning) {
123            mTriggerPercentage = 0;
124            mFinishTime = AnimationUtils.currentAnimationTimeMillis();
125            mRunning = false;
126            mParent.postInvalidate();
127        }
128    }
129
130    /**
131     * @return Return whether the progress animation is currently running.
132     */
133    boolean isRunning() {
134        return mRunning || mFinishTime > 0;
135    }
136
137    void draw(Canvas canvas) {
138        final int width = mBounds.width();
139        final int height = mBounds.height();
140        final int cx = width / 2;
141        final int cy = height / 2;
142        boolean drawTriggerWhileFinishing = false;
143        int restoreCount = canvas.save();
144        canvas.clipRect(mBounds);
145
146        if (mRunning || (mFinishTime > 0)) {
147            long now = AnimationUtils.currentAnimationTimeMillis();
148            long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
149            long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
150            float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));
151
152            // If we're not running anymore, that means we're running through
153            // the finish animation.
154            if (!mRunning) {
155                // If the finish animation is done, don't draw anything, and
156                // don't repost.
157                if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
158                    mFinishTime = 0;
159                    return;
160                }
161
162                // Otherwise, use a 0 opacity alpha layer to clear the animation
163                // from the inside out. This layer will prevent the circles from
164                // drawing within its bounds.
165                long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
166                float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
167                float pct = (finishProgress / 100f);
168                // Radius of the circle is half of the screen.
169                float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
170                mClipRect.set(cx - clearRadius, 0, cx + clearRadius, height);
171                canvas.saveLayerAlpha(mClipRect, 0, 0);
172                // Only draw the trigger if there is a space in the center of
173                // this refreshing view that needs to be filled in by the
174                // trigger. If the progress view is just still animating, let it
175                // continue animating.
176                drawTriggerWhileFinishing = true;
177            }
178
179            // First fill in with the last color that would have finished drawing.
180            if (iterations == 0) {
181                canvas.drawColor(mColor1);
182            } else {
183                if (rawProgress >= 0 && rawProgress < 25) {
184                    canvas.drawColor(mColor4);
185                } else if (rawProgress >= 25 && rawProgress < 50) {
186                    canvas.drawColor(mColor1);
187                } else if (rawProgress >= 50 && rawProgress < 75) {
188                    canvas.drawColor(mColor2);
189                } else {
190                    canvas.drawColor(mColor3);
191                }
192            }
193
194            // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
195            // along we are in the cycle.
196            // progress 0-50 draw mColor2
197            // progress 25-75 draw mColor3
198            // progress 50-100 draw mColor4
199            // progress 75 (wrap to 25) draw mColor1
200            if ((rawProgress >= 0 && rawProgress <= 25)) {
201                float pct = (((rawProgress + 25) * 2) / 100f);
202                drawCircle(canvas, cx, cy, mColor1, pct);
203            }
204            if (rawProgress >= 0 && rawProgress <= 50) {
205                float pct = ((rawProgress * 2) / 100f);
206                drawCircle(canvas, cx, cy, mColor2, pct);
207            }
208            if (rawProgress >= 25 && rawProgress <= 75) {
209                float pct = (((rawProgress - 25) * 2) / 100f);
210                drawCircle(canvas, cx, cy, mColor3, pct);
211            }
212            if (rawProgress >= 50 && rawProgress <= 100) {
213                float pct = (((rawProgress - 50) * 2) / 100f);
214                drawCircle(canvas, cx, cy, mColor4, pct);
215            }
216            if ((rawProgress >= 75 && rawProgress <= 100)) {
217                float pct = (((rawProgress - 75) * 2) / 100f);
218                drawCircle(canvas, cx, cy, mColor1, pct);
219            }
220            if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
221                // There is some portion of trigger to draw. Restore the canvas,
222                // then draw the trigger. Otherwise, the trigger does not appear
223                // until after the bar has finished animating and appears to
224                // just jump in at a larger width than expected.
225                canvas.restoreToCount(restoreCount);
226                restoreCount = canvas.save();
227                canvas.clipRect(mBounds);
228                drawTrigger(canvas, cx, cy);
229            }
230            // Keep running until we finish out the last cycle.
231            ViewCompat.postInvalidateOnAnimation(
232                    mParent, mBounds.left, mBounds.top, mBounds.right, mBounds.bottom);
233        } else {
234            // Otherwise if we're in the middle of a trigger, draw that.
235            if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
236                drawTrigger(canvas, cx, cy);
237            }
238        }
239        canvas.restoreToCount(restoreCount);
240    }
241
242    private void drawTrigger(Canvas canvas, int cx, int cy) {
243        mPaint.setColor(mColor1);
244        canvas.drawCircle(cx, cy, cx * mTriggerPercentage, mPaint);
245    }
246
247    /**
248     * Draws a circle centered in the view.
249     *
250     * @param canvas the canvas to draw on
251     * @param cx the center x coordinate
252     * @param cy the center y coordinate
253     * @param color the color to draw
254     * @param pct the percentage of the view that the circle should cover
255     */
256    private void drawCircle(Canvas canvas, float cx, float cy, int color, float pct) {
257        mPaint.setColor(color);
258        canvas.save();
259        canvas.translate(cx, cy);
260        float radiusScale = INTERPOLATOR.getInterpolation(pct);
261        canvas.scale(radiusScale, radiusScale);
262        canvas.drawCircle(0, 0, cx, mPaint);
263        canvas.restore();
264    }
265
266    /**
267     * Set the drawing bounds of this SwipeProgressBar.
268     */
269    void setBounds(int left, int top, int right, int bottom) {
270        mBounds.left = left;
271        mBounds.top = top;
272        mBounds.right = right;
273        mBounds.bottom = bottom;
274    }
275}