summaryrefslogtreecommitdiffstats
path: root/tests/UiBench/src/com/android/test/uibench/InvalidateActivity.java
blob: 93d67a60515f62d92a8d8de6bbd2285d22670c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.test.uibench;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Tests invalidation performance by invalidating a large number of easily rendered views,
 */
public class InvalidateActivity extends AppCompatActivity {
    public static class ColorView extends View {
        @ColorInt
        public int mColor;

        public ColorView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public void setColor(@ColorInt int color) {
            mColor = color;
            invalidate();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(mColor);
        }
    }

    ColorView[][] mColorViews;

    @SuppressWarnings("unused")
    public void setColorValue(int colorValue) {
        @ColorInt int a = Color.rgb(colorValue, 255 - colorValue, 255);
        @ColorInt int b = Color.rgb(255, colorValue, 255 - colorValue);
        for (int y = 0; y < mColorViews.length; y++) {
            for (int x = 0; x < mColorViews[y].length; x++) {
                mColorViews[y][x].setColor((x + y) % 2 == 0 ? a : b);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invalidate);

        ViewGroup root = (ViewGroup) findViewById(R.id.invalidate_root);
        for (int y = 0; y < root.getChildCount(); y++) {
            ViewGroup row = (ViewGroup) root.getChildAt(y);
            if (mColorViews == null) {
                mColorViews = new ColorView[root.getChildCount()][row.getChildCount()];
            }

            for (int x = 0; x < row.getChildCount(); x++) {
                mColorViews[y][x] = (ColorView) row.getChildAt(x);
            }
        }

        ObjectAnimator animator = ObjectAnimator.ofInt(this, "colorValue", 0, 255);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }
}