| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
public 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 |
} |