Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2009 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.cooliris.media;
     18 
     19 import android.util.FloatMath;
     20 
     21 public final class FloatAnim {
     22     private float mValue;
     23     private float mDelta;
     24     private float mDuration;
     25     private long mStartTime;
     26 
     27     public FloatAnim(float value) {
     28         mValue = value;
     29         mStartTime = 0;
     30     }
     31 
     32     public boolean isAnimating() {
     33         return mStartTime != 0;
     34     }
     35 
     36     public float getTimeRemaining(long currentTime) {
     37         float duration = (currentTime - mStartTime) * 0.001f;
     38         if (mDuration > duration) // CR: braces
     39             return mDuration - duration;
     40         else
     41             return 0.0f;
     42     }
     43 
     44     public float getValue(long currentTime) {
     45         if (mStartTime == 0) {
     46             return mValue;
     47         } else {
     48             return getInterpolatedValue(currentTime);
     49         }
     50     }
     51 
     52     public void animateValue(float value, float duration, long currentTime) {
     53         mDelta = getValue(currentTime) - value;
     54         mValue = value;
     55         mDuration = duration;
     56         mStartTime = currentTime;
     57     }
     58 
     59     public void setValue(float value) {
     60         mValue = value;
     61         mStartTime = 0;
     62     }
     63 
     64     public void skip() {
     65         mStartTime = 0;
     66     }
     67 
     68     private float getInterpolatedValue(long currentTime) {
     69         float ratio = (float) (currentTime - mStartTime) * 0.001f / mDuration;
     70         if (ratio >= 1f) { // CR: 1.0f
     71             mStartTime = 0;
     72             return mValue;
     73         } else {
     74             ratio = 0.5f - 0.5f * FloatMath.cos(ratio * 3.14159265f); // CR:
     75                                                                       // (float)Math.PI
     76             return mValue + (1f - ratio) * mDelta;
     77         }
     78     }
     79 }
     80