• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.test.uibench;
17 
18 import android.animation.ObjectAnimator;
19 import android.animation.ValueAnimator;
20 import android.graphics.Color;
21 import android.os.Bundle;
22 import androidx.appcompat.app.AppCompatActivity;
23 import android.view.ViewGroup;
24 import android.widget.LinearLayout;
25 
26 import java.util.ArrayList;
27 
28 /**
29  * Tests invalidation/record performance by invalidating a large number of easily rendered
30  * nested views.
31  */
32 public class InvalidateTreeActivity extends AppCompatActivity {
33     private final ArrayList<LinearLayout> mLayouts = new ArrayList<>();
34 
35     private int mColorToggle = 0;
36     private ObjectAnimator mAnimator;
37 
createQuadTree(LinearLayout parent, int remainingDepth)38     private void createQuadTree(LinearLayout parent, int remainingDepth) {
39         mLayouts.add(parent);
40         if (remainingDepth <= 0) {
41             mColorToggle = (mColorToggle + 1) % 4;
42             parent.setBackgroundColor((mColorToggle < 2) ? Color.RED : Color.BLUE);
43             return;
44         }
45 
46         boolean vertical = remainingDepth % 2 == 0;
47         parent.setOrientation(vertical ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);
48 
49         for (int i = 0; i < 2; i++) {
50             LinearLayout child = new LinearLayout(this);
51             // vertical: match parent in x axis, horizontal: y axis.
52             parent.addView(child, new LinearLayout.LayoutParams(
53                     (vertical ? ViewGroup.LayoutParams.MATCH_PARENT : 0),
54                     (vertical ? 0 : ViewGroup.LayoutParams.MATCH_PARENT),
55                     1.0f));
56 
57             createQuadTree(child, remainingDepth - 1);
58         }
59     }
60 
61     @SuppressWarnings("unused")
62     public void setIgnoredValue(int ignoredValue) {
63         for (int i = 0; i < mLayouts.size(); i++) {
64             mLayouts.get(i).invalidate();
65         }
66     }
67 
68     @Override
69     protected void onCreate(Bundle savedInstanceState) {
70         super.onCreate(savedInstanceState);
71         LinearLayout root = new LinearLayout(this);
72         createQuadTree(root, 8);
73         setContentView(root);
74 
75         mAnimator = ObjectAnimator.ofInt(this, "ignoredValue", 0, 1000);
76         mAnimator.setRepeatMode(ValueAnimator.REVERSE);
77         mAnimator.setRepeatCount(ValueAnimator.INFINITE);
78         mAnimator.start();
79     }
80 
81     @Override
82     protected void onPause() {
83         super.onPause();
84         if (mAnimator != null) {
85             mAnimator.cancel();
86         }
87     }
88 }
89