Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e1352de6 authored by ztenghui's avatar ztenghui Committed by Android (Google) Code Review
Browse files

Merge "Setup the animation callback for AnimatedVectorDrawable" into mnc-dev

parents 3e85c1ed 83a52031
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -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 {
+16 −4
Original line number Diff line number Diff line
@@ -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 {
+61 −0
Original line number Diff line number Diff line
/*
 * 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) {};
    }
}
+80 −33
Original line number Diff line number Diff line
@@ -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
+9 −16
Original line number Diff line number Diff line
@@ -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,23 +56,16 @@ public class AnimatedVectorDrawableTest extends Activity implements View.OnClick
            button.setHeight(400);
            button.setBackgroundResource(icon[i]);
            AnimatedVectorDrawable d = (AnimatedVectorDrawable) button.getBackground();
            d.addListener(new AnimatorListener() {
            d.registerAnimationCallback(new Animatable2.AnimationCallback() {
                @Override
                public void onAnimationStart(Animator animation) {
                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) {
                public void onAnimationEnd(Drawable drawable) {
                        Log.v(LOGCAT, "Animator end");
                }
                    @Override
                public void onAnimationCancel(Animator animation) {
                    Log.v(LOGCAT, "Animator cancel");
                }
            });

            container.addView(button);