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