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 package com.android.test.uibench; 17 18 import android.animation.ObjectAnimator; 19 import android.animation.ValueAnimator; 20 import android.content.Context; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.os.Bundle; 24 import androidx.annotation.ColorInt; 25 import androidx.appcompat.app.AppCompatActivity; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 /** 31 * Tests invalidation performance by invalidating a large number of easily rendered views, 32 */ 33 public class InvalidateActivity extends AppCompatActivity { 34 private static class ColorView extends View { 35 @ColorInt 36 public int mColor; 37 ColorView(Context context, AttributeSet attrs)38 public ColorView(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 setColor(@olorInt int color)42 public void setColor(@ColorInt int color) { 43 mColor = color; 44 invalidate(); 45 } 46 47 @Override onDraw(Canvas canvas)48 protected void onDraw(Canvas canvas) { 49 canvas.drawColor(mColor); 50 } 51 } 52 53 private ColorView[][] mColorViews; 54 55 @SuppressWarnings("unused") setColorValue(int colorValue)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 onCreate(Bundle savedInstanceState)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