15a1089de6e705823009189c75c630c12d0350921ztenghui/* 25a1089de6e705823009189c75c630c12d0350921ztenghui * Copyright (C) 2017 The Android Open Source Project 35a1089de6e705823009189c75c630c12d0350921ztenghui * 45a1089de6e705823009189c75c630c12d0350921ztenghui * Licensed under the Apache License, Version 2.0 (the "License"); 55a1089de6e705823009189c75c630c12d0350921ztenghui * you may not use this file except in compliance with the License. 65a1089de6e705823009189c75c630c12d0350921ztenghui * You may obtain a copy of the License at 75a1089de6e705823009189c75c630c12d0350921ztenghui * 85a1089de6e705823009189c75c630c12d0350921ztenghui * http://www.apache.org/licenses/LICENSE-2.0 95a1089de6e705823009189c75c630c12d0350921ztenghui * 105a1089de6e705823009189c75c630c12d0350921ztenghui * Unless required by applicable law or agreed to in writing, software 115a1089de6e705823009189c75c630c12d0350921ztenghui * distributed under the License is distributed on an "AS IS" BASIS, 125a1089de6e705823009189c75c630c12d0350921ztenghui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135a1089de6e705823009189c75c630c12d0350921ztenghui * See the License for the specific language governing permissions and 145a1089de6e705823009189c75c630c12d0350921ztenghui * limitations under the License. 155a1089de6e705823009189c75c630c12d0350921ztenghui */ 165a1089de6e705823009189c75c630c12d0350921ztenghui 175a1089de6e705823009189c75c630c12d0350921ztenghuipackage com.example.android.support.vectordrawable.app; 185a1089de6e705823009189c75c630c12d0350921ztenghui 195a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.graphics.drawable.Animatable; 205a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.graphics.drawable.Drawable; 215a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.os.Bundle; 225a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.support.graphics.drawable.Animatable2Compat; 235a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.support.graphics.drawable.AnimatedVectorDrawableCompat; 245a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.support.v7.app.AppCompatActivity; 255a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.support.v7.widget.AppCompatImageView; 265a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.view.MotionEvent; 275a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.view.View; 285a1089de6e705823009189c75c630c12d0350921ztenghuiimport android.widget.TextView; 295a1089de6e705823009189c75c630c12d0350921ztenghui 305a1089de6e705823009189c75c630c12d0350921ztenghuiimport com.example.android.support.vectordrawable.R; 315a1089de6e705823009189c75c630c12d0350921ztenghui 325a1089de6e705823009189c75c630c12d0350921ztenghui/** 335a1089de6e705823009189c75c630c12d0350921ztenghui * A demo for AnimatedVectorDrawableCompat's listener support. 345a1089de6e705823009189c75c630c12d0350921ztenghui */ 355a1089de6e705823009189c75c630c12d0350921ztenghuipublic class AVDCListenerDemo extends AppCompatActivity { 365a1089de6e705823009189c75c630c12d0350921ztenghui 375a1089de6e705823009189c75c630c12d0350921ztenghui @Override 385a1089de6e705823009189c75c630c12d0350921ztenghui protected void onCreate(Bundle savedInstanceState) { 395a1089de6e705823009189c75c630c12d0350921ztenghui super.onCreate(savedInstanceState); 405a1089de6e705823009189c75c630c12d0350921ztenghui setContentView(R.layout.avdc_listener); 41fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final AppCompatImageView imageView1 = findViewById(R.id.imageView); 42fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final AppCompatImageView imageView2 = findViewById(R.id.imageView2); 435a1089de6e705823009189c75c630c12d0350921ztenghui 44fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final TextView textView1 = findViewById(R.id.textView); 455a1089de6e705823009189c75c630c12d0350921ztenghui textView1.setText("Should show start / end for first AVD"); 46fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final TextView textView2 = findViewById(R.id.textView2); 475a1089de6e705823009189c75c630c12d0350921ztenghui textView2.setText("Not affected by AVD, b/c removed after register"); 48fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final TextView textView3 = findViewById(R.id.textView3); 495a1089de6e705823009189c75c630c12d0350921ztenghui textView3.setText("Should show start / end for second AVD"); 50fa2e2acf79d791a90410025daad438968550d18cAlan Viverette final TextView textView4 = findViewById(R.id.textView4); 515a1089de6e705823009189c75c630c12d0350921ztenghui textView4.setText("Not affected by AVD, b/c unregistered after register"); 525a1089de6e705823009189c75c630c12d0350921ztenghui 535a1089de6e705823009189c75c630c12d0350921ztenghui final Drawable drawable1 = imageView1.getDrawable(); 545a1089de6e705823009189c75c630c12d0350921ztenghui final Drawable drawable2 = imageView2.getDrawable(); 555a1089de6e705823009189c75c630c12d0350921ztenghui 565a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback textView1Callback = new 575a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback() { 585a1089de6e705823009189c75c630c12d0350921ztenghui @Override 595a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationStart(Drawable drawable) { 605a1089de6e705823009189c75c630c12d0350921ztenghui textView1.setText("AVD 1 started"); 615a1089de6e705823009189c75c630c12d0350921ztenghui } 625a1089de6e705823009189c75c630c12d0350921ztenghui 635a1089de6e705823009189c75c630c12d0350921ztenghui @Override 645a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationEnd(Drawable drawable) { 655a1089de6e705823009189c75c630c12d0350921ztenghui textView1.setText("AVD 1 Ended"); 665a1089de6e705823009189c75c630c12d0350921ztenghui } 675a1089de6e705823009189c75c630c12d0350921ztenghui }; 685a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback textView2Callback = new 695a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback() { 705a1089de6e705823009189c75c630c12d0350921ztenghui @Override 715a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationStart(Drawable drawable) { 725a1089de6e705823009189c75c630c12d0350921ztenghui textView2.setText("AVD 1 started"); 735a1089de6e705823009189c75c630c12d0350921ztenghui } 745a1089de6e705823009189c75c630c12d0350921ztenghui 755a1089de6e705823009189c75c630c12d0350921ztenghui @Override 765a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationEnd(Drawable drawable) { 775a1089de6e705823009189c75c630c12d0350921ztenghui textView2.setText("AVD 1 Ended"); 785a1089de6e705823009189c75c630c12d0350921ztenghui } 795a1089de6e705823009189c75c630c12d0350921ztenghui }; 805a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView1Callback); 815a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView2Callback); 825a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.clearAnimationCallbacks(drawable1); 835a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.registerAnimationCallback(drawable1, textView1Callback); 845a1089de6e705823009189c75c630c12d0350921ztenghui 855a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.registerAnimationCallback(drawable2, 865a1089de6e705823009189c75c630c12d0350921ztenghui new Animatable2Compat.AnimationCallback() { 875a1089de6e705823009189c75c630c12d0350921ztenghui @Override 885a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationStart(Drawable drawable) { 895a1089de6e705823009189c75c630c12d0350921ztenghui textView3.setText("AVD 2 started"); 905a1089de6e705823009189c75c630c12d0350921ztenghui } 915a1089de6e705823009189c75c630c12d0350921ztenghui 925a1089de6e705823009189c75c630c12d0350921ztenghui @Override 935a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationEnd(Drawable drawable) { 945a1089de6e705823009189c75c630c12d0350921ztenghui textView3.setText("AVD 2 Ended"); 955a1089de6e705823009189c75c630c12d0350921ztenghui } 965a1089de6e705823009189c75c630c12d0350921ztenghui }); 975a1089de6e705823009189c75c630c12d0350921ztenghui 985a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback textView4Callback = new 995a1089de6e705823009189c75c630c12d0350921ztenghui Animatable2Compat.AnimationCallback() { 1005a1089de6e705823009189c75c630c12d0350921ztenghui @Override 1015a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationStart(Drawable drawable) { 1025a1089de6e705823009189c75c630c12d0350921ztenghui textView4.setText("AVD 2 started"); 1035a1089de6e705823009189c75c630c12d0350921ztenghui } 1045a1089de6e705823009189c75c630c12d0350921ztenghui 1055a1089de6e705823009189c75c630c12d0350921ztenghui @Override 1065a1089de6e705823009189c75c630c12d0350921ztenghui public void onAnimationEnd(Drawable drawable) { 1075a1089de6e705823009189c75c630c12d0350921ztenghui textView4.setText("AVD 2 Ended"); 1085a1089de6e705823009189c75c630c12d0350921ztenghui } 1095a1089de6e705823009189c75c630c12d0350921ztenghui }; 1105a1089de6e705823009189c75c630c12d0350921ztenghui 1115a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.registerAnimationCallback(drawable2, textView4Callback); 1125a1089de6e705823009189c75c630c12d0350921ztenghui AnimatedVectorDrawableCompat.unregisterAnimationCallback(drawable2, textView4Callback); 1135a1089de6e705823009189c75c630c12d0350921ztenghui 1145a1089de6e705823009189c75c630c12d0350921ztenghui // Touch the imageView will run the AVD. 1155a1089de6e705823009189c75c630c12d0350921ztenghui imageView1.setOnTouchListener(new View.OnTouchListener() { 1165a1089de6e705823009189c75c630c12d0350921ztenghui @Override 1175a1089de6e705823009189c75c630c12d0350921ztenghui public boolean onTouch(View v, MotionEvent event) { 1185a1089de6e705823009189c75c630c12d0350921ztenghui if (!((Animatable) drawable1).isRunning()) { 1195a1089de6e705823009189c75c630c12d0350921ztenghui ((Animatable) drawable1).start(); 1205a1089de6e705823009189c75c630c12d0350921ztenghui } 1215a1089de6e705823009189c75c630c12d0350921ztenghui return true; 1225a1089de6e705823009189c75c630c12d0350921ztenghui } 1235a1089de6e705823009189c75c630c12d0350921ztenghui }); 1245a1089de6e705823009189c75c630c12d0350921ztenghui 1255a1089de6e705823009189c75c630c12d0350921ztenghui imageView2.setOnTouchListener(new View.OnTouchListener() { 1265a1089de6e705823009189c75c630c12d0350921ztenghui @Override 1275a1089de6e705823009189c75c630c12d0350921ztenghui public boolean onTouch(View v, MotionEvent event) { 1285a1089de6e705823009189c75c630c12d0350921ztenghui if (!((Animatable) drawable2).isRunning()) { 1295a1089de6e705823009189c75c630c12d0350921ztenghui ((Animatable) drawable2).start(); 1305a1089de6e705823009189c75c630c12d0350921ztenghui } 1315a1089de6e705823009189c75c630c12d0350921ztenghui return true; 1325a1089de6e705823009189c75c630c12d0350921ztenghui } 1335a1089de6e705823009189c75c630c12d0350921ztenghui }); 1345a1089de6e705823009189c75c630c12d0350921ztenghui } 1355a1089de6e705823009189c75c630c12d0350921ztenghui} 136