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 */
16package com.android.test.uibench;
17
18import android.animation.ObjectAnimator;
19import android.animation.ValueAnimator;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.os.Bundle;
24import android.support.annotation.ColorInt;
25import android.support.v7.app.AppCompatActivity;
26import android.util.AttributeSet;
27import android.view.View;
28import android.view.ViewGroup;
29
30/**
31 * Tests invalidation performance by invalidating a large number of easily rendered views,
32 */
33public class InvalidateActivity extends AppCompatActivity {
34    private static class ColorView extends View {
35        @ColorInt
36        public int mColor;
37
38        public ColorView(Context context, AttributeSet attrs) {
39            super(context, attrs);
40        }
41
42        public void setColor(@ColorInt int color) {
43            mColor = color;
44            invalidate();
45        }
46
47        @Override
48        protected void onDraw(Canvas canvas) {
49            canvas.drawColor(mColor);
50        }
51    }
52
53    private ColorView[][] mColorViews;
54
55    @SuppressWarnings("unused")
56    public void setColorValue(int colorValue) {
57        @ColorInt int a = Color.rgb(colorValue, 255 - colorValue, 255);
58        @ColorInt int b = Color.rgb(255, colorValue, 255 - colorValue);
59        for (int y = 0; y < mColorViews.length; y++) {
60            for (int x = 0; x < mColorViews[y].length; x++) {
61                mColorViews[y][x].setColor((x + y) % 2 == 0 ? a : b);
62            }
63        }
64    }
65
66    @Override
67    protected void onCreate(Bundle savedInstanceState) {
68        super.onCreate(savedInstanceState);
69        setContentView(R.layout.activity_invalidate);
70
71        ViewGroup root = (ViewGroup) findViewById(R.id.invalidate_root);
72        for (int y = 0; y < root.getChildCount(); y++) {
73            ViewGroup row = (ViewGroup) root.getChildAt(y);
74            if (mColorViews == null) {
75                mColorViews = new ColorView[root.getChildCount()][row.getChildCount()];
76            }
77
78            for (int x = 0; x < row.getChildCount(); x++) {
79                mColorViews[y][x] = (ColorView) row.getChildAt(x);
80            }
81        }
82
83        ObjectAnimator animator = ObjectAnimator.ofInt(this, "colorValue", 0, 255);
84        animator.setRepeatMode(ValueAnimator.REVERSE);
85        animator.setRepeatCount(ValueAnimator.INFINITE);
86        animator.start();
87    }
88}
89