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