summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/anim
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/gallery3d/anim')
-rw-r--r--src/com/android/gallery3d/anim/AlphaAnimation.java48
-rw-r--r--src/com/android/gallery3d/anim/Animation.java92
-rw-r--r--src/com/android/gallery3d/anim/CanvasAnimation.java25
-rw-r--r--src/com/android/gallery3d/anim/FloatAnimation.java40
-rw-r--r--src/com/android/gallery3d/anim/StateTransitionAnimation.java180
5 files changed, 385 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/anim/AlphaAnimation.java b/src/com/android/gallery3d/anim/AlphaAnimation.java
new file mode 100644
index 000000000..f9f4cbd2c
--- /dev/null
+++ b/src/com/android/gallery3d/anim/AlphaAnimation.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2010 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.gallery3d.anim;
+
+import com.android.gallery3d.common.Utils;
+import com.android.gallery3d.glrenderer.GLCanvas;
+
+public class AlphaAnimation extends CanvasAnimation {
+ private final float mStartAlpha;
+ private final float mEndAlpha;
+ private float mCurrentAlpha;
+
+ public AlphaAnimation(float from, float to) {
+ mStartAlpha = from;
+ mEndAlpha = to;
+ mCurrentAlpha = from;
+ }
+
+ @Override
+ public void apply(GLCanvas canvas) {
+ canvas.multiplyAlpha(mCurrentAlpha);
+ }
+
+ @Override
+ public int getCanvasSaveFlags() {
+ return GLCanvas.SAVE_FLAG_ALPHA;
+ }
+
+ @Override
+ protected void onCalculate(float progress) {
+ mCurrentAlpha = Utils.clamp(mStartAlpha
+ + (mEndAlpha - mStartAlpha) * progress, 0f, 1f);
+ }
+}
diff --git a/src/com/android/gallery3d/anim/Animation.java b/src/com/android/gallery3d/anim/Animation.java
new file mode 100644
index 000000000..cc117bbce
--- /dev/null
+++ b/src/com/android/gallery3d/anim/Animation.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2010 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.gallery3d.anim;
+
+import android.view.animation.Interpolator;
+
+import com.android.gallery3d.common.Utils;
+
+// Animation calculates a value according to the current input time.
+//
+// 1. First we need to use setDuration(int) to set the duration of the
+// animation. The duration is in milliseconds.
+// 2. Then we should call start(). The actual start time is the first value
+// passed to calculate(long).
+// 3. Each time we want to get an animation value, we call
+// calculate(long currentTimeMillis) to ask the Animation to calculate it.
+// The parameter passed to calculate(long) should be nonnegative.
+// 4. Use get() to get that value.
+//
+// In step 3, onCalculate(float progress) is called so subclasses can calculate
+// the value according to progress (progress is a value in [0,1]).
+//
+// Before onCalculate(float) is called, There is an optional interpolator which
+// can change the progress value. The interpolator can be set by
+// setInterpolator(Interpolator). If the interpolator is used, the value passed
+// to onCalculate may be (for example, the overshoot effect).
+//
+// The isActive() method returns true after the animation start() is called and
+// before calculate is passed a value which reaches the duration of the
+// animation.
+//
+// The start() method can be called again to restart the Animation.
+//
+abstract public class Animation {
+ private static final long ANIMATION_START = -1;
+ private static final long NO_ANIMATION = -2;
+
+ private long mStartTime = NO_ANIMATION;
+ private int mDuration;
+ private Interpolator mInterpolator;
+
+ public void setInterpolator(Interpolator interpolator) {
+ mInterpolator = interpolator;
+ }
+
+ public void setDuration(int duration) {
+ mDuration = duration;
+ }
+
+ public void start() {
+ mStartTime = ANIMATION_START;
+ }
+
+ public void setStartTime(long time) {
+ mStartTime = time;
+ }
+
+ public boolean isActive() {
+ return mStartTime != NO_ANIMATION;
+ }
+
+ public void forceStop() {
+ mStartTime = NO_ANIMATION;
+ }
+
+ public boolean calculate(long currentTimeMillis) {
+ if (mStartTime == NO_ANIMATION) return false;
+ if (mStartTime == ANIMATION_START) mStartTime = currentTimeMillis;
+ int elapse = (int) (currentTimeMillis - mStartTime);
+ float x = Utils.clamp((float) elapse / mDuration, 0f, 1f);
+ Interpolator i = mInterpolator;
+ onCalculate(i != null ? i.getInterpolation(x) : x);
+ if (elapse >= mDuration) mStartTime = NO_ANIMATION;
+ return mStartTime != NO_ANIMATION;
+ }
+
+ abstract protected void onCalculate(float progress);
+}
diff --git a/src/com/android/gallery3d/anim/CanvasAnimation.java b/src/com/android/gallery3d/anim/CanvasAnimation.java
new file mode 100644
index 000000000..cdc66c6ba
--- /dev/null
+++ b/src/com/android/gallery3d/anim/CanvasAnimation.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2010 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.gallery3d.anim;
+
+import com.android.gallery3d.glrenderer.GLCanvas;
+
+public abstract class CanvasAnimation extends Animation {
+
+ public abstract int getCanvasSaveFlags();
+ public abstract void apply(GLCanvas canvas);
+}
diff --git a/src/com/android/gallery3d/anim/FloatAnimation.java b/src/com/android/gallery3d/anim/FloatAnimation.java
new file mode 100644
index 000000000..1294ec2f4
--- /dev/null
+++ b/src/com/android/gallery3d/anim/FloatAnimation.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 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.gallery3d.anim;
+
+public class FloatAnimation extends Animation {
+
+ private final float mFrom;
+ private final float mTo;
+ private float mCurrent;
+
+ public FloatAnimation(float from, float to, int duration) {
+ mFrom = from;
+ mTo = to;
+ mCurrent = from;
+ setDuration(duration);
+ }
+
+ @Override
+ protected void onCalculate(float progress) {
+ mCurrent = mFrom + (mTo - mFrom) * progress;
+ }
+
+ public float get() {
+ return mCurrent;
+ }
+}
diff --git a/src/com/android/gallery3d/anim/StateTransitionAnimation.java b/src/com/android/gallery3d/anim/StateTransitionAnimation.java
new file mode 100644
index 000000000..bf8a54405
--- /dev/null
+++ b/src/com/android/gallery3d/anim/StateTransitionAnimation.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2012 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.gallery3d.anim;
+
+import android.view.animation.AccelerateInterpolator;
+import android.view.animation.DecelerateInterpolator;
+import android.view.animation.Interpolator;
+
+import com.android.gallery3d.glrenderer.GLCanvas;
+import com.android.gallery3d.glrenderer.RawTexture;
+import com.android.gallery3d.ui.GLView;
+import com.android.gallery3d.ui.TiledScreenNail;
+
+public class StateTransitionAnimation extends Animation {
+
+ public static class Spec {
+ public static final Spec OUTGOING;
+ public static final Spec INCOMING;
+ public static final Spec PHOTO_INCOMING;
+
+ private static final Interpolator DEFAULT_INTERPOLATOR =
+ new DecelerateInterpolator();
+
+ public int duration = 330;
+ public float backgroundAlphaFrom = 0;
+ public float backgroundAlphaTo = 0;
+ public float backgroundScaleFrom = 0;
+ public float backgroundScaleTo = 0;
+ public float contentAlphaFrom = 1;
+ public float contentAlphaTo = 1;
+ public float contentScaleFrom = 1;
+ public float contentScaleTo = 1;
+ public float overlayAlphaFrom = 0;
+ public float overlayAlphaTo = 0;
+ public float overlayScaleFrom = 0;
+ public float overlayScaleTo = 0;
+ public Interpolator interpolator = DEFAULT_INTERPOLATOR;
+
+ static {
+ OUTGOING = new Spec();
+ OUTGOING.backgroundAlphaFrom = 0.5f;
+ OUTGOING.backgroundAlphaTo = 0f;
+ OUTGOING.backgroundScaleFrom = 1f;
+ OUTGOING.backgroundScaleTo = 0f;
+ OUTGOING.contentAlphaFrom = 0.5f;
+ OUTGOING.contentAlphaTo = 1f;
+ OUTGOING.contentScaleFrom = 3f;
+ OUTGOING.contentScaleTo = 1f;
+
+ INCOMING = new Spec();
+ INCOMING.overlayAlphaFrom = 1f;
+ INCOMING.overlayAlphaTo = 0f;
+ INCOMING.overlayScaleFrom = 1f;
+ INCOMING.overlayScaleTo = 3f;
+ INCOMING.contentAlphaFrom = 0f;
+ INCOMING.contentAlphaTo = 1f;
+ INCOMING.contentScaleFrom = 0.25f;
+ INCOMING.contentScaleTo = 1f;
+
+ PHOTO_INCOMING = INCOMING;
+ }
+
+ private static Spec specForTransition(Transition t) {
+ switch (t) {
+ case Outgoing:
+ return Spec.OUTGOING;
+ case Incoming:
+ return Spec.INCOMING;
+ case PhotoIncoming:
+ return Spec.PHOTO_INCOMING;
+ case None:
+ default:
+ return null;
+ }
+ }
+ }
+
+ public static enum Transition { None, Outgoing, Incoming, PhotoIncoming }
+
+ private final Spec mTransitionSpec;
+ private float mCurrentContentScale;
+ private float mCurrentContentAlpha;
+ private float mCurrentBackgroundScale;
+ private float mCurrentBackgroundAlpha;
+ private float mCurrentOverlayScale;
+ private float mCurrentOverlayAlpha;
+ private RawTexture mOldScreenTexture;
+
+ public StateTransitionAnimation(Transition t, RawTexture oldScreen) {
+ this(Spec.specForTransition(t), oldScreen);
+ }
+
+ public StateTransitionAnimation(Spec spec, RawTexture oldScreen) {
+ mTransitionSpec = spec != null ? spec : Spec.OUTGOING;
+ setDuration(mTransitionSpec.duration);
+ setInterpolator(mTransitionSpec.interpolator);
+ mOldScreenTexture = oldScreen;
+ TiledScreenNail.disableDrawPlaceholder();
+ }
+
+ @Override
+ public boolean calculate(long currentTimeMillis) {
+ boolean retval = super.calculate(currentTimeMillis);
+ if (!isActive()) {
+ if (mOldScreenTexture != null) {
+ mOldScreenTexture.recycle();
+ mOldScreenTexture = null;
+ }
+ TiledScreenNail.enableDrawPlaceholder();
+ }
+ return retval;
+ }
+
+ @Override
+ protected void onCalculate(float progress) {
+ mCurrentContentScale = mTransitionSpec.contentScaleFrom
+ + (mTransitionSpec.contentScaleTo - mTransitionSpec.contentScaleFrom) * progress;
+ mCurrentContentAlpha = mTransitionSpec.contentAlphaFrom
+ + (mTransitionSpec.contentAlphaTo - mTransitionSpec.contentAlphaFrom) * progress;
+ mCurrentBackgroundAlpha = mTransitionSpec.backgroundAlphaFrom
+ + (mTransitionSpec.backgroundAlphaTo - mTransitionSpec.backgroundAlphaFrom)
+ * progress;
+ mCurrentBackgroundScale = mTransitionSpec.backgroundScaleFrom
+ + (mTransitionSpec.backgroundScaleTo - mTransitionSpec.backgroundScaleFrom)
+ * progress;
+ mCurrentOverlayScale = mTransitionSpec.overlayScaleFrom
+ + (mTransitionSpec.overlayScaleTo - mTransitionSpec.overlayScaleFrom) * progress;
+ mCurrentOverlayAlpha = mTransitionSpec.overlayAlphaFrom
+ + (mTransitionSpec.overlayAlphaTo - mTransitionSpec.overlayAlphaFrom) * progress;
+ }
+
+ private void applyOldTexture(GLView view, GLCanvas canvas, float alpha, float scale, boolean clear) {
+ if (mOldScreenTexture == null)
+ return;
+ if (clear) canvas.clearBuffer(view.getBackgroundColor());
+ canvas.save();
+ canvas.setAlpha(alpha);
+ int xOffset = view.getWidth() / 2;
+ int yOffset = view.getHeight() / 2;
+ canvas.translate(xOffset, yOffset);
+ canvas.scale(scale, scale, 1);
+ mOldScreenTexture.draw(canvas, -xOffset, -yOffset);
+ canvas.restore();
+ }
+
+ public void applyBackground(GLView view, GLCanvas canvas) {
+ if (mCurrentBackgroundAlpha > 0f) {
+ applyOldTexture(view, canvas, mCurrentBackgroundAlpha, mCurrentBackgroundScale, true);
+ }
+ }
+
+ public void applyContentTransform(GLView view, GLCanvas canvas) {
+ int xOffset = view.getWidth() / 2;
+ int yOffset = view.getHeight() / 2;
+ canvas.translate(xOffset, yOffset);
+ canvas.scale(mCurrentContentScale, mCurrentContentScale, 1);
+ canvas.translate(-xOffset, -yOffset);
+ canvas.setAlpha(mCurrentContentAlpha);
+ }
+
+ public void applyOverlay(GLView view, GLCanvas canvas) {
+ if (mCurrentOverlayAlpha > 0f) {
+ applyOldTexture(view, canvas, mCurrentOverlayAlpha, mCurrentOverlayScale, false);
+ }
+ }
+}