1/*
2 * Copyright (C) 2010 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 com.android.gallery3d.ui;
18
19import com.android.gallery3d.R;
20
21import android.content.Context;
22
23public class ProgressSpinner {
24    private static float ROTATE_SPEED_OUTER = 1080f / 3500f;
25    private static float ROTATE_SPEED_INNER = -720f / 3500f;
26    private final ResourceTexture mOuter;
27    private final ResourceTexture mInner;
28    private final int mWidth;
29    private final int mHeight;
30
31    private float mInnerDegree = 0f;
32    private float mOuterDegree = 0f;
33    private long mAnimationTimestamp = -1;
34
35    public ProgressSpinner(Context context) {
36        mOuter = new ResourceTexture(context, R.drawable.spinner_76_outer_holo);
37        mInner = new ResourceTexture(context, R.drawable.spinner_76_inner_holo);
38
39        mWidth = Math.max(mOuter.getWidth(), mInner.getWidth());
40        mHeight = Math.max(mOuter.getHeight(), mInner.getHeight());
41    }
42
43    public int getWidth() {
44        return mWidth;
45    }
46
47    public int getHeight() {
48        return mHeight;
49    }
50
51    public void startAnimation() {
52        mAnimationTimestamp = -1;
53        mOuterDegree = 0;
54        mInnerDegree = 0;
55    }
56
57    public void draw(GLCanvas canvas, int x, int y) {
58        long now = canvas.currentAnimationTimeMillis();
59        if (mAnimationTimestamp == -1) mAnimationTimestamp = now;
60        mOuterDegree += (now - mAnimationTimestamp) * ROTATE_SPEED_OUTER;
61        mInnerDegree += (now - mAnimationTimestamp) * ROTATE_SPEED_INNER;
62
63        mAnimationTimestamp = now;
64
65        // just preventing overflow
66        if (mOuterDegree > 360) mOuterDegree -= 360f;
67        if (mInnerDegree < 0) mInnerDegree += 360f;
68
69        canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
70
71        canvas.translate(x + mWidth / 2, y + mHeight / 2, 0);
72        canvas.rotate(mInnerDegree, 0, 0, 1);
73        mOuter.draw(canvas, -mOuter.getWidth() / 2, -mOuter.getHeight() / 2);
74        canvas.rotate(mOuterDegree - mInnerDegree, 0, 0, 1);
75        mInner.draw(canvas, -mInner.getWidth() / 2, -mInner.getHeight() / 2);
76        canvas.restore();
77    }
78}
79