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