Animatable2Compat.java revision b31c3281d870e9abb673db239234d580dcc4feff
1/* 2 * Copyright (C) 2017 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 17package androidx.vectordrawable.graphics.drawable; 18 19import static android.os.Build.VERSION_CODES.M; 20 21import android.graphics.drawable.Animatable; 22import android.graphics.drawable.Animatable2; 23import android.graphics.drawable.Drawable; 24import androidx.annotation.NonNull; 25import androidx.annotation.RequiresApi; 26 27/** 28 * Interface that drawables supporting animations and callbacks should extend in support lib. 29 */ 30public interface Animatable2Compat extends Animatable { 31 32 /** 33 * Adds a callback to listen to the animation events. 34 * 35 * @param callback Callback to add. 36 */ 37 void registerAnimationCallback(@NonNull AnimationCallback callback); 38 39 /** 40 * Removes the specified animation callback. 41 * 42 * @param callback Callback to remove. 43 * @return {@code false} if callback didn't exist in the call back list, or {@code true} if 44 * callback has been removed successfully. 45 */ 46 boolean unregisterAnimationCallback(@NonNull AnimationCallback callback); 47 48 /** 49 * Removes all existing animation callbacks. 50 */ 51 void clearAnimationCallbacks(); 52 53 /** 54 * Abstract class for animation callback. Used to notify animation events. 55 */ 56 abstract class AnimationCallback { 57 /** 58 * Called when the animation starts. 59 * 60 * @param drawable The drawable started the animation. 61 */ 62 public void onAnimationStart(Drawable drawable) {}; 63 /** 64 * Called when the animation ends. 65 * 66 * @param drawable The drawable finished the animation. 67 */ 68 public void onAnimationEnd(Drawable drawable) {}; 69 70 // Only when passing this Animatable2Compat.AnimationCallback to a frameworks' AVD, we need 71 // to bridge this compat version callback with the frameworks' callback. 72 Animatable2.AnimationCallback mPlatformCallback; 73 74 @RequiresApi(M) 75 Animatable2.AnimationCallback getPlatformCallback() { 76 if (mPlatformCallback == null) { 77 mPlatformCallback = new Animatable2.AnimationCallback() { 78 @Override 79 public void onAnimationStart(Drawable drawable) { 80 AnimationCallback.this.onAnimationStart(drawable); 81 } 82 83 @Override 84 public void onAnimationEnd(Drawable drawable) { 85 AnimationCallback.this.onAnimationEnd(drawable); 86 } 87 }; 88 } 89 return mPlatformCallback; 90 } 91 } 92} 93