ViewInvertHelper.java revision c18010f6720f606003cde3cd376ddacaca30f6e5
1/* 2 * Copyright (C) 2014 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 17package com.android.systemui; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.ValueAnimator; 22import android.content.Context; 23import android.graphics.ColorMatrix; 24import android.graphics.ColorMatrixColorFilter; 25import android.graphics.Paint; 26import android.view.View; 27 28import com.android.systemui.statusbar.Interpolators; 29 30import java.util.ArrayList; 31 32/** 33 * Helper to invert the colors of views and fade between the states. 34 */ 35public class ViewInvertHelper { 36 37 private final Paint mDarkPaint = new Paint(); 38 private final ColorMatrix mMatrix = new ColorMatrix(); 39 private final ColorMatrix mGrayscaleMatrix = new ColorMatrix(); 40 private final long mFadeDuration; 41 private final ArrayList<View> mTargets = new ArrayList<>(); 42 43 public ViewInvertHelper(View v, long fadeDuration) { 44 this(v.getContext(), fadeDuration); 45 addTarget(v); 46 } 47 public ViewInvertHelper(Context context, long fadeDuration) { 48 mFadeDuration = fadeDuration; 49 } 50 51 private static ArrayList<View> constructArray(View target) { 52 final ArrayList<View> views = new ArrayList<>(); 53 views.add(target); 54 return views; 55 } 56 57 public void clearTargets() { 58 mTargets.clear(); 59 } 60 61 public void addTarget(View target) { 62 mTargets.add(target); 63 } 64 65 public void fade(final boolean invert, long delay) { 66 float startIntensity = invert ? 0f : 1f; 67 float endIntensity = invert ? 1f : 0f; 68 ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity); 69 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 70 @Override 71 public void onAnimationUpdate(ValueAnimator animation) { 72 updateInvertPaint((Float) animation.getAnimatedValue()); 73 for (int i = 0; i < mTargets.size(); i++) { 74 mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint); 75 } 76 } 77 }); 78 animator.addListener(new AnimatorListenerAdapter() { 79 @Override 80 public void onAnimationEnd(Animator animation) { 81 if (!invert) { 82 for (int i = 0; i < mTargets.size(); i++) { 83 mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null); 84 } 85 } 86 } 87 }); 88 animator.setDuration(mFadeDuration); 89 animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); 90 animator.setStartDelay(delay); 91 animator.start(); 92 } 93 94 public void update(boolean invert) { 95 if (invert) { 96 updateInvertPaint(1f); 97 for (int i = 0; i < mTargets.size(); i++) { 98 mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint); 99 } 100 } else { 101 for (int i = 0; i < mTargets.size(); i++) { 102 mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null); 103 } 104 } 105 } 106 107 private void updateInvertPaint(float intensity) { 108 float components = 1 - 2 * intensity; 109 final float[] invert = { 110 components, 0f, 0f, 0f, 255f * intensity, 111 0f, components, 0f, 0f, 255f * intensity, 112 0f, 0f, components, 0f, 255f * intensity, 113 0f, 0f, 0f, 1f, 0f 114 }; 115 mMatrix.set(invert); 116 mGrayscaleMatrix.setSaturation(1 - intensity); 117 mMatrix.preConcat(mGrayscaleMatrix); 118 mDarkPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix)); 119 } 120 121 public void setInverted(boolean invert, boolean fade, long delay) { 122 if (fade) { 123 fade(invert, delay); 124 } else { 125 update(invert); 126 } 127 } 128}