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