diff options
author | Joe Onorato <joeo@android.com> | 2009-08-01 00:01:20 -0700 |
---|---|---|
committer | Joe Onorato <joeo@android.com> | 2009-08-01 00:01:20 -0700 |
commit | e77c08d15f23c403293dbb40c6a36967de822c89 (patch) | |
tree | 0e89947ebd345e4d3934aa8686686cecf3b13aa3 /src/com/android/launcher2/SymmetricalLinearTween.java | |
parent | 5979117dc26fe8939e7af3116401b4ea93a0adb2 (diff) | |
download | android_packages_apps_Trebuchet-e77c08d15f23c403293dbb40c6a36967de822c89.tar.gz android_packages_apps_Trebuchet-e77c08d15f23c403293dbb40c6a36967de822c89.tar.bz2 android_packages_apps_Trebuchet-e77c08d15f23c403293dbb40c6a36967de822c89.zip |
Fade the workspace when the all apps drawer opens and closes.
Diffstat (limited to 'src/com/android/launcher2/SymmetricalLinearTween.java')
-rw-r--r-- | src/com/android/launcher2/SymmetricalLinearTween.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/com/android/launcher2/SymmetricalLinearTween.java b/src/com/android/launcher2/SymmetricalLinearTween.java new file mode 100644 index 000000000..2e0ed8f03 --- /dev/null +++ b/src/com/android/launcher2/SymmetricalLinearTween.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher2; + +import android.os.Handler; +import android.os.Message; +import android.os.SystemClock; +import android.util.Log; + +/** + * Provides an animation between 0.0f and 1.0f over a given duration. + */ +class SymmetricalLinearTween { + + private static final int FPS = 30; + private static final int FRAME_TIME = 1000 / FPS; + + Handler mHandler; + int mDuration; + TweenCallback mCallback; + + boolean mRunning; + long mBase; + boolean mDirection; + float mValue; + + /** + * @param duration milliseconds duration + * @param callback callbacks + */ + public SymmetricalLinearTween(boolean initial, int duration, TweenCallback callback) { + mValue = initial ? 1.0f : 0.0f; + mDirection = initial; + mDuration = duration; + mCallback = callback; + mHandler = new Handler(); + } + + /** + * Starts the tweening. + * + * @param direction If direction is true, the value goes towards 1.0f. If direction + * is false, the value goes towards 0.0f. + */ + public void start(boolean direction) { + start(direction, SystemClock.uptimeMillis()); + } + + /** + * Starts the tweening. + * + * @param direction If direction is true, the value goes towards 1.0f. If direction + * is false, the value goes towards 0.0f. + * @param baseTime The time to use as zero for this animation, in the + * {@link SystemClock.uptimeMillis} time base. This allows you to + * synchronize multiple animations. + */ + public void start(boolean direction, long baseTime) { + if (direction != mDirection) { + if (!mRunning) { + mBase = baseTime; + mRunning = true; + mCallback.onTweenStarted(); + long next = SystemClock.uptimeMillis() + FRAME_TIME; + mHandler.postAtTime(mTick, next); + } else { + // reverse direction + long now = SystemClock.uptimeMillis(); + long diff = now - mBase; + mBase = now + diff - mDuration; + } + mDirection = direction; + } + } + + Runnable mTick = new Runnable() { + public void run() { + long base = mBase; + long now = SystemClock.uptimeMillis(); + long diff = now-base; + int duration = mDuration; + float val = diff/(float)duration; + if (!mDirection) { + val = 1.0f - val; + } + if (val > 1.0f) { + val = 1.0f; + } else if (val < 0.0f) { + val = 0.0f; + } + float old = mValue; + mValue = val; + mCallback.onTweenValueChanged(val, old); + int frame = (int)(diff / FRAME_TIME); + long next = base + ((frame+1)*FRAME_TIME); + if (diff < duration) { + mHandler.postAtTime(this, next); + } + if (diff >= duration) { + mCallback.onTweenFinished(); + mRunning = false; + } + } + }; +} + |