blob: 4f9381c76a7e3849a0e603a584835c066c48522b [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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
Owen Lina2fba682011-08-17 22:07:43 +080019import android.content.Context;
20
Owen Lin73a04ff2012-03-14 17:27:24 +080021import com.android.gallery3d.R;
22
Owen Lina2fba682011-08-17 22:07:43 +080023public 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) {
Chih-Chung Changb3d01962012-02-17 10:02:27 +080058 long now = AnimationTime.get();
Owen Lina2fba682011-08-17 22:07:43 +080059 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
Chih-Chung Chang3f43ecb2012-02-16 07:27:03 +080071 canvas.translate(x + mWidth / 2, y + mHeight / 2);
Owen Lina2fba682011-08-17 22:07:43 +080072 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}