1 /* 2 * Copyright (C) 2015 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.os.Bundle; 20 import android.view.View; 21 import android.widget.Button; 22 import android.widget.LinearLayout; 23 import android.widget.ScrollView; 24 import android.widget.TextView; 25 26 import androidx.appcompat.app.AppCompatActivity; 27 import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat; 28 29 import com.example.android.support.vectordrawable.R; 30 31 import java.text.DecimalFormat; 32 33 /** 34 * Simple demo for AnimatedVectorDrawableCompat. 35 */ 36 public class SimpleAnimatedVectorDrawable extends AppCompatActivity implements 37 View.OnClickListener { 38 protected int[] mIcons = { 39 R.drawable.animation_vector_drawable_grouping_1_path_motion, 40 R.drawable.animation_vector_drawable_grouping_1_path_motion_object, 41 R.drawable.animation_vector_drawable_grouping_1, 42 R.drawable.animation_vector_drawable_grouping_decelerate, 43 R.drawable.animation_vector_drawable_grouping_accelerate, 44 R.drawable.ic_hourglass_animation, 45 R.drawable.ic_signal_airplane_v2_animation, 46 R.drawable.animation_vector_progress_bar, 47 R.drawable.btn_radio_on_to_off_bundle, 48 }; 49 50 @Override onCreate(Bundle savedInstanceState)51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 ScrollView scrollView = new ScrollView(this); 54 LinearLayout container = new LinearLayout(this); 55 scrollView.addView(container); 56 container.setOrientation(LinearLayout.VERTICAL); 57 container.setBackgroundColor(0xFF888888); 58 AnimatedVectorDrawableCompat[] d = new AnimatedVectorDrawableCompat[mIcons.length]; 59 long time = android.os.SystemClock.currentThreadTimeMillis(); 60 for (int i = 0; i < mIcons.length; i++) { 61 d[i] = AnimatedVectorDrawableCompat.create(this, mIcons[i]); 62 } 63 time = android.os.SystemClock.currentThreadTimeMillis() - time; 64 TextView t = new TextView(this); 65 DecimalFormat df = new DecimalFormat("#.##"); 66 t.setText("avgL=" + df.format(time / mIcons.length) + " ms"); 67 container.addView(t); 68 69 addDrawableButtons(container, d); 70 71 // Now test constant state and mutate a bit. 72 if (d[0].getConstantState() != null) { 73 AnimatedVectorDrawableCompat[] copies = new AnimatedVectorDrawableCompat[3]; 74 copies[0] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable(); 75 copies[1] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable(); 76 copies[2] = (AnimatedVectorDrawableCompat) d[0].getConstantState().newDrawable(); 77 copies[0].setAlpha(128); 78 79 // Expect to see the copies[0, 1] are showing alpha 128, and [2] are showing 255. 80 copies[2].mutate(); 81 copies[2].setAlpha(255); 82 83 addDrawableButtons(container, copies); 84 } 85 86 setContentView(scrollView); 87 } 88 addDrawableButtons(LinearLayout container, AnimatedVectorDrawableCompat[] d)89 private void addDrawableButtons(LinearLayout container, AnimatedVectorDrawableCompat[] d) { 90 for (int i = 0; i < d.length; i++) { 91 Button button = new Button(this); 92 button.setWidth(200); 93 button.setHeight(200); 94 button.setBackgroundDrawable(d[i]); 95 container.addView(button); 96 button.setOnClickListener(this); 97 } 98 } 99 100 @Override onClick(View v)101 public void onClick(View v) { 102 AnimatedVectorDrawableCompat d = (AnimatedVectorDrawableCompat) v.getBackground(); 103 d.start(); 104 } 105 } 106