diff options
author | ztenghui <ztenghui@google.com> | 2015-05-29 17:19:41 -0700 |
---|---|---|
committer | ztenghui <ztenghui@google.com> | 2015-06-04 12:55:21 -0700 |
commit | 83a52031fd5c277d0c6e75da50bf8013e8a70399 (patch) | |
tree | 7d1cf90ee2ccead94abe77cee440fc64a7d7b88f | |
parent | 1da56b51d59aa4c587dcdb5bf4c1719961be0ae7 (diff) | |
download | frameworks_base-83a52031fd5c277d0c6e75da50bf8013e8a70399.zip frameworks_base-83a52031fd5c277d0c6e75da50bf8013e8a70399.tar.gz frameworks_base-83a52031fd5c277d0c6e75da50bf8013e8a70399.tar.bz2 |
Setup the animation callback for AnimatedVectorDrawable
b/21341096
Change-Id: I84e20366db21ceaa4f044be3e322f9215bb06ad2
-rw-r--r-- | api/current.txt | 20 | ||||
-rw-r--r-- | api/system-current.txt | 20 | ||||
-rw-r--r-- | graphics/java/android/graphics/drawable/Animatable2.java | 61 | ||||
-rw-r--r-- | graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java | 113 | ||||
-rw-r--r-- | tests/VectorDrawableTest/src/com/android/test/dynamic/AnimatedVectorDrawableTest.java | 25 |
5 files changed, 182 insertions, 57 deletions
diff --git a/api/current.txt b/api/current.txt index 0467b69..0b6c9e7 100644 --- a/api/current.txt +++ b/api/current.txt @@ -12166,24 +12166,36 @@ package android.graphics.drawable { method public abstract void stop(); } + public abstract interface Animatable2 implements android.graphics.drawable.Animatable { + method public abstract void clearAnimationCallbacks(); + method public abstract void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); + method public abstract boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); + } + + public static abstract class Animatable2.AnimationCallback { + ctor public Animatable2.AnimationCallback(); + method public void onAnimationEnd(android.graphics.drawable.Drawable); + method public void onAnimationStart(android.graphics.drawable.Drawable); + } + public class AnimatedStateListDrawable extends android.graphics.drawable.StateListDrawable { ctor public AnimatedStateListDrawable(); method public void addState(int[], android.graphics.drawable.Drawable, int); method public void addTransition(int, int, T, boolean); } - public class AnimatedVectorDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Animatable { + public class AnimatedVectorDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Animatable2 { ctor public AnimatedVectorDrawable(); - method public void addListener(android.animation.Animator.AnimatorListener); + method public void clearAnimationCallbacks(); method public void draw(android.graphics.Canvas); - method public java.util.List<android.animation.Animator.AnimatorListener> getListeners(); method public int getOpacity(); method public boolean isRunning(); - method public void removeListener(android.animation.Animator.AnimatorListener); + method public void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); method public void setAlpha(int); method public void setColorFilter(android.graphics.ColorFilter); method public void start(); method public void stop(); + method public boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); } public class AnimationDrawable extends android.graphics.drawable.DrawableContainer implements android.graphics.drawable.Animatable java.lang.Runnable { diff --git a/api/system-current.txt b/api/system-current.txt index d11fe85..f80030f 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -12479,24 +12479,36 @@ package android.graphics.drawable { method public abstract void stop(); } + public abstract interface Animatable2 implements android.graphics.drawable.Animatable { + method public abstract void clearAnimationCallbacks(); + method public abstract void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); + method public abstract boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); + } + + public static abstract class Animatable2.AnimationCallback { + ctor public Animatable2.AnimationCallback(); + method public void onAnimationEnd(android.graphics.drawable.Drawable); + method public void onAnimationStart(android.graphics.drawable.Drawable); + } + public class AnimatedStateListDrawable extends android.graphics.drawable.StateListDrawable { ctor public AnimatedStateListDrawable(); method public void addState(int[], android.graphics.drawable.Drawable, int); method public void addTransition(int, int, T, boolean); } - public class AnimatedVectorDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Animatable { + public class AnimatedVectorDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Animatable2 { ctor public AnimatedVectorDrawable(); - method public void addListener(android.animation.Animator.AnimatorListener); + method public void clearAnimationCallbacks(); method public void draw(android.graphics.Canvas); - method public java.util.List<android.animation.Animator.AnimatorListener> getListeners(); method public int getOpacity(); method public boolean isRunning(); - method public void removeListener(android.animation.Animator.AnimatorListener); + method public void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); method public void setAlpha(int); method public void setColorFilter(android.graphics.ColorFilter); method public void start(); method public void stop(); + method public boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback); } public class AnimationDrawable extends android.graphics.drawable.DrawableContainer implements android.graphics.drawable.Animatable java.lang.Runnable { diff --git a/graphics/java/android/graphics/drawable/Animatable2.java b/graphics/java/android/graphics/drawable/Animatable2.java new file mode 100644 index 0000000..7c7e60e --- /dev/null +++ b/graphics/java/android/graphics/drawable/Animatable2.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2015 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 android.graphics.drawable; + +import android.annotation.NonNull; + +/** + * Abstract class that drawables supporting animations and callbacks should extend. + */ +public interface Animatable2 extends Animatable { + + /** + * Adds a callback to listen to the animation events. + * + * @param callback Callback to add. + */ + void registerAnimationCallback(@NonNull AnimationCallback callback); + + /** + * Removes the specified animation callback. + * + * @param callback Callback to remove. + * @return {@code false} if callback didn't exist in the call back list, or {@code true} if + * callback has been removed successfully. + */ + boolean unregisterAnimationCallback(@NonNull AnimationCallback callback); + + /** + * Removes all existing animation callbacks. + */ + void clearAnimationCallbacks(); + + public static abstract class AnimationCallback { + /** + * Called when the animation starts. + * + * @param drawable The drawable started the animation. + */ + public void onAnimationStart(Drawable drawable) {}; + /** + * Called when the animation ends. + * + * @param drawable The drawable finished the animation. + */ + public void onAnimationEnd(Drawable drawable) {}; + } +} diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java index 100c2f4..96f86b4 100644 --- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java @@ -16,6 +16,7 @@ package android.graphics.drawable; import android.animation.Animator; import android.animation.AnimatorInflater; +import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.Animator.AnimatorListener; import android.annotation.NonNull; @@ -42,7 +43,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; -import java.util.List; /** * This class uses {@link android.animation.ObjectAnimator} and @@ -129,7 +129,7 @@ import java.util.List; * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_name * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_animation */ -public class AnimatedVectorDrawable extends Drawable implements Animatable { +public class AnimatedVectorDrawable extends Drawable implements Animatable2 { private static final String LOGTAG = "AnimatedVectorDrawable"; private static final String ANIMATED_VECTOR = "animated-vector"; @@ -153,6 +153,10 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable { private boolean mMutated; + /** Use a internal AnimatorListener to support callbacks during animation events. */ + private ArrayList<Animatable2.AnimationCallback> mAnimationCallbacks = null; + private AnimatorListener mAnimatorListener = null; + public AnimatedVectorDrawable() { this(null, null); } @@ -380,36 +384,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable { } } - /** - * Adds a listener to the set of listeners that are sent events through the life of an - * animation. - * - * @param listener the listener to be added to the current set of listeners for this animation. - */ - public void addListener(AnimatorListener listener) { - mAnimatorSet.addListener(listener); - } - - /** - * Removes a listener from the set listening to this animation. - * - * @param listener the listener to be removed from the current set of listeners for this - * animation. - */ - public void removeListener(AnimatorListener listener) { - mAnimatorSet.removeListener(listener); - } - - /** - * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently - * listening for events on this <code>AnimatedVectorDrawable</code> object. - * - * @return List<AnimatorListener> The set of listeners. - */ - public List<AnimatorListener> getListeners() { - return mAnimatorSet.getListeners(); - } - private static class AnimatedVectorDrawableState extends ConstantState { int mChangingConfigurations; VectorDrawable mVectorDrawable; @@ -674,4 +648,77 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable { unscheduleSelf(what); } }; -} + + @Override + public void registerAnimationCallback(@NonNull AnimationCallback callback) { + if (callback == null) { + return; + } + + // Add listener accordingly. + if (mAnimationCallbacks == null) { + mAnimationCallbacks = new ArrayList<>(); + } + + mAnimationCallbacks.add(callback); + + if (mAnimatorListener == null) { + // Create a animator listener and trigger the callback events when listener is + // triggered. + mAnimatorListener = new AnimatorListenerAdapter() { + @Override + public void onAnimationStart(Animator animation) { + ArrayList<AnimationCallback> tmpCallbacks = new ArrayList<>(mAnimationCallbacks); + int size = tmpCallbacks.size(); + for (int i = 0; i < size; i ++) { + tmpCallbacks.get(i).onAnimationStart(AnimatedVectorDrawable.this); + } + } + + @Override + public void onAnimationEnd(Animator animation) { + ArrayList<AnimationCallback> tmpCallbacks = new ArrayList<>(mAnimationCallbacks); + int size = tmpCallbacks.size(); + for (int i = 0; i < size; i ++) { + tmpCallbacks.get(i).onAnimationEnd(AnimatedVectorDrawable.this); + } + } + }; + } + mAnimatorSet.addListener(mAnimatorListener); + } + + // A helper function to clean up the animator listener in the mAnimatorSet. + private void removeAnimatorSetListener() { + if (mAnimatorListener != null) { + mAnimatorSet.removeListener(mAnimatorListener); + mAnimatorListener = null; + } + } + + @Override + public boolean unregisterAnimationCallback(@NonNull AnimationCallback callback) { + if (mAnimationCallbacks == null || callback == null) { + // Nothing to be removed. + return false; + } + boolean removed = mAnimationCallbacks.remove(callback); + + // When the last call back unregistered, remove the listener accordingly. + if (mAnimationCallbacks.size() == 0) { + removeAnimatorSetListener(); + } + return removed; + } + + @Override + public void clearAnimationCallbacks() { + removeAnimatorSetListener(); + if (mAnimationCallbacks == null) { + return; + } + + mAnimationCallbacks.clear(); + } + +}
\ No newline at end of file diff --git a/tests/VectorDrawableTest/src/com/android/test/dynamic/AnimatedVectorDrawableTest.java b/tests/VectorDrawableTest/src/com/android/test/dynamic/AnimatedVectorDrawableTest.java index 56c8119..087e68a 100644 --- a/tests/VectorDrawableTest/src/com/android/test/dynamic/AnimatedVectorDrawableTest.java +++ b/tests/VectorDrawableTest/src/com/android/test/dynamic/AnimatedVectorDrawableTest.java @@ -14,10 +14,10 @@ package com.android.test.dynamic; -import android.animation.Animator; -import android.animation.Animator.AnimatorListener; import android.app.Activity; +import android.graphics.drawable.Animatable2; import android.graphics.drawable.AnimatedVectorDrawable; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -56,22 +56,15 @@ public class AnimatedVectorDrawableTest extends Activity implements View.OnClick button.setHeight(400); button.setBackgroundResource(icon[i]); AnimatedVectorDrawable d = (AnimatedVectorDrawable) button.getBackground(); - d.addListener(new AnimatorListener() { - @Override - public void onAnimationStart(Animator animation) { + d.registerAnimationCallback(new Animatable2.AnimationCallback() { + @Override + public void onAnimationStart(Drawable drawable) { Log.v(LOGCAT, "Animator start"); } - @Override - public void onAnimationRepeat(Animator animation) { - Log.v(LOGCAT, "Animator repeat"); - } - @Override - public void onAnimationEnd(Animator animation) { - Log.v(LOGCAT, "Animator end"); - } - @Override - public void onAnimationCancel(Animator animation) { - Log.v(LOGCAT, "Animator cancel"); + + @Override + public void onAnimationEnd(Drawable drawable) { + Log.v(LOGCAT, "Animator end"); } }); |