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 17 package com.example.android.support.vectordrawable.app; 18 19 import android.graphics.drawable.Animatable; 20 import android.graphics.drawable.Drawable; 21 import android.os.Bundle; 22 import android.view.MotionEvent; 23 import android.view.View; 24 import android.widget.TextView; 25 26 import androidx.appcompat.app.AppCompatActivity; 27 import androidx.appcompat.widget.AppCompatImageView; 28 import androidx.vectordrawable.graphics.drawable.Animatable2Compat; 29 import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat; 30 31 import com.example.android.support.vectordrawable.R; 32 33 import org.jspecify.annotations.NonNull; 34 35 /** 36 * A demo for AnimatedVectorDrawableCompat's listener support. 37 */ 38 public class AVDCListenerDemo extends AppCompatActivity { 39 40 @Override onCreate(Bundle savedInstanceState)41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.avdc_listener); 44 final AppCompatImageView imageView1 = findViewById(R.id.imageView); 45 final AppCompatImageView imageView2 = findViewById(R.id.imageView2); 46 47 final TextView textView1 = findViewById(R.id.textView); 48 textView1.setText("Should show start / end for first AVD"); 49 final TextView textView2 = findViewById(R.id.textView2); 50 textView2.setText("Not affected by AVD, b/c removed after register"); 51 final TextView textView3 = findViewById(R.id.textView3); 52 textView3.setText("Should show start / end for second AVD"); 53 final TextView textView4 = findViewById(R.id.textView4); 54 textView4.setText("Not affected by AVD, b/c unregistered after register"); 55 56 final Drawable drawable1 = imageView1.getDrawable(); 57 final Drawable drawable2 = imageView2.getDrawable(); 58 59 Animatable2Compat.AnimationCallback textView1Callback = new 60 Animatable2Compat.AnimationCallback() { 61 @Override 62 public void onAnimationStart(@NonNull Drawable drawable) { 63 textView1.setText("AVD 1 started"); 64 } 65 66 @Override 67 public void onAnimationEnd(@NonNull Drawable drawable) { 68 textView1.setText("AVD 1 Ended"); 69 } 70 }; 71 Animatable2Compat.AnimationCallback textView2Callback = new 72 Animatable2Compat.AnimationCallback() { 73 @Override 74 public void onAnimationStart(@NonNull Drawable drawable) { 75 textView2.setText("AVD 1 started"); 76 } 77 78 @Override 79 public void onAnimationEnd(@NonNull Drawable drawable) { 80 textView2.setText("AVD 1 Ended"); 81 } 82 }; 83 AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView1Callback); 84 AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView2Callback); 85 AnimatedVectorDrawableCompat.clearAnimationCallbacks(drawable1); 86 AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView1Callback); 87 88 AnimatedVectorDrawableCompat.registerAnimationCallback(drawable2, 89 new Animatable2Compat.AnimationCallback() { 90 @Override 91 public void onAnimationStart(@NonNull Drawable drawable) { 92 textView3.setText("AVD 2 started"); 93 } 94 95 @Override 96 public void onAnimationEnd(@NonNull Drawable drawable) { 97 textView3.setText("AVD 2 Ended"); 98 } 99 }); 100 101 Animatable2Compat.AnimationCallback textView4Callback = new 102 Animatable2Compat.AnimationCallback() { 103 @Override 104 public void onAnimationStart(@NonNull Drawable drawable) { 105 textView4.setText("AVD 2 started"); 106 } 107 108 @Override 109 public void onAnimationEnd(@NonNull Drawable drawable) { 110 textView4.setText("AVD 2 Ended"); 111 } 112 }; 113 114 AnimatedVectorDrawableCompat.registerAnimationCallback(drawable2, textView4Callback); 115 AnimatedVectorDrawableCompat.unregisterAnimationCallback(drawable2, textView4Callback); 116 117 // Touch the imageView will run the AVD. 118 imageView1.setOnTouchListener(new View.OnTouchListener() { 119 @Override 120 public boolean onTouch(View v, MotionEvent event) { 121 if (!((Animatable) drawable1).isRunning()) { 122 ((Animatable) drawable1).start(); 123 } 124 return true; 125 } 126 }); 127 128 imageView2.setOnTouchListener(new View.OnTouchListener() { 129 @Override 130 public boolean onTouch(View v, MotionEvent event) { 131 if (!((Animatable) drawable2).isRunning()) { 132 ((Animatable) drawable2).start(); 133 } 134 return true; 135 } 136 }); 137 } 138 } 139