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