1/*
2 * Copyright (C) 2012 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 */
16package com.android.test.hwui;
17
18import android.animation.ObjectAnimator;
19import android.animation.ValueAnimator;
20import android.app.Activity;
21import android.content.Context;
22import android.os.Bundle;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.animation.AlphaAnimation;
26import android.view.animation.Animation;
27import android.view.animation.Transformation;
28import android.view.animation.TranslateAnimation;
29import android.widget.Button;
30import android.widget.CheckBox;
31import android.widget.CompoundButton;
32import android.widget.LinearLayout;
33
34public class TransformsAndAnimationsActivity extends Activity {
35    Button button1;
36    Button button2;
37    Button button3;
38    Button button1a;
39    Button button2a;
40    Button button3a;
41    Button button1b;
42    Button button2b;
43    Button button3b;
44    Button button4;
45    Button button5;
46    Button button6;
47    Button button7;
48    Button button8;
49    CheckBox layersNoneCB;
50    CheckBox layersHardwareCB;
51    CheckBox layersSoftwareCB;
52
53    @Override
54    protected void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        setContentView(R.layout.transforms_and_animations);
57
58        button1 = (Button) findViewById(R.id.button1);
59        button2 = (Button) findViewById(R.id.button2);
60        button3 = (Button) findViewById(R.id.button3);
61        button1a = (Button) findViewById(R.id.button1a);
62        button2a = (Button) findViewById(R.id.button2a);
63        button3a = (Button) findViewById(R.id.button3a);
64        button1b = (Button) findViewById(R.id.button1b);
65        button2b = (Button) findViewById(R.id.button2b);
66        button3b = (Button) findViewById(R.id.button3b);
67        button4 = (Button) findViewById(R.id.button4);
68        button5 = (Button) findViewById(R.id.button5);
69        button6 = (Button) findViewById(R.id.button6);
70        button7 = (Button) findViewById(R.id.button7);
71        button8 = (Button) findViewById(R.id.button8);
72        layersNoneCB = (CheckBox) findViewById(R.id.layersNoneCB);
73        layersHardwareCB = (CheckBox) findViewById(R.id.layersHwCB);
74        layersSoftwareCB = (CheckBox) findViewById(R.id.layersSwCB);
75
76        layersNoneCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
77            @Override
78            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
79                if (isChecked) {
80                    setLayerType(View.LAYER_TYPE_NONE);
81                    layersHardwareCB.setChecked(false);
82                    layersSoftwareCB.setChecked(false);
83                }
84            }
85        });
86
87        layersSoftwareCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
88            @Override
89            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
90                if (isChecked) {
91                    setLayerType(View.LAYER_TYPE_SOFTWARE);
92                    layersHardwareCB.setChecked(false);
93                    layersNoneCB.setChecked(false);
94                }
95            }
96        });
97
98        layersHardwareCB.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
99            @Override
100            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
101                if (isChecked) {
102                    setLayerType(View.LAYER_TYPE_HARDWARE);
103                    layersNoneCB.setChecked(false);
104                    layersSoftwareCB.setChecked(false);
105                }
106            }
107        });
108
109        button1a.setAlpha(.5f);
110        button2a.setAlpha(.5f);
111        button3a.setAlpha(.5f);
112        button3.setTranslationX(50);
113        button7.setTranslationX(50);
114        button8.setTranslationX(50);
115
116        final AlphaAnimation alphaAnim = new AlphaAnimation(1, 0);
117        alphaAnim.setDuration(1000);
118        alphaAnim.setRepeatCount(Animation.INFINITE);
119        alphaAnim.setRepeatMode(Animation.REVERSE);
120
121        final TranslateAnimation transAnim = new TranslateAnimation(0, -50, 0, 0);
122        transAnim.setDuration(1000);
123        transAnim.setRepeatCount(Animation.INFINITE);
124        transAnim.setRepeatMode(Animation.REVERSE);
125
126        getWindow().getDecorView().postDelayed(new Runnable() {
127            @Override
128            public void run() {
129                button1.startAnimation(alphaAnim);
130                button2.startAnimation(alphaAnim);
131                button3.startAnimation(alphaAnim);
132
133                button1a.startAnimation(alphaAnim);
134                button2a.startAnimation(alphaAnim);
135                button3a.startAnimation(alphaAnim);
136
137                button1b.startAnimation(alphaAnim);
138                button2b.startAnimation(alphaAnim);
139                button3b.startAnimation(alphaAnim);
140                startAnimator(button1b);
141                startAnimator(button2b);
142                startAnimator(button3b);
143
144                button7.startAnimation(transAnim);
145                button8.startAnimation(transAnim);
146            }
147        }, 2000);
148    }
149
150    private void setLayerType(int layerType) {
151        button1.setLayerType(layerType, null);
152        button2.setLayerType(layerType, null);
153        button3.setLayerType(layerType, null);
154        button1a.setLayerType(layerType, null);
155        button2a.setLayerType(layerType, null);
156        button3a.setLayerType(layerType, null);
157        button1b.setLayerType(layerType, null);
158        button2b.setLayerType(layerType, null);
159        button3b.setLayerType(layerType, null);
160        button4.setLayerType(layerType, null);
161        button5.setLayerType(layerType, null);
162        button6.setLayerType(layerType, null);
163        button7.setLayerType(layerType, null);
164        button8.setLayerType(layerType, null);
165    }
166
167    private void startAnimator(View target) {
168        ObjectAnimator anim1b = ObjectAnimator.ofFloat(target, View.ALPHA, 0);
169        anim1b.setRepeatCount(ValueAnimator.INFINITE);
170        anim1b.setRepeatMode(ValueAnimator.REVERSE);
171        anim1b.setDuration(1000);
172        anim1b.start();
173    }
174
175    public static class MyLayout extends LinearLayout {
176
177        public MyLayout(Context context) {
178            super(context);
179            setStaticTransformationsEnabled(true);
180        }
181
182        public MyLayout(Context context, AttributeSet attrs) {
183            super(context, attrs);
184            setStaticTransformationsEnabled(true);
185        }
186
187        public MyLayout(Context context, AttributeSet attrs, int defStyle) {
188            super(context, attrs, defStyle);
189            setStaticTransformationsEnabled(true);
190        }
191
192        @Override
193        protected boolean getChildStaticTransformation(View child, Transformation t) {
194            t.clear();
195            t.setAlpha(.35f);
196
197            return true;
198        }
199    }
200}
201
202