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 
17 package com.android.systemui;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ValueAnimator;
22 import android.graphics.ColorMatrix;
23 import android.graphics.ColorMatrixColorFilter;
24 import android.graphics.Paint;
25 import android.view.View;
26 import android.view.animation.AnimationUtils;
27 import android.view.animation.Interpolator;
28 
29 /**
30  * Helper to invert the colors of views and fade between the states.
31  */
32 public class ViewInvertHelper {
33 
34     private final Paint mDarkPaint = new Paint();
35     private final Interpolator mLinearOutSlowInInterpolator;
36     private final View mTarget;
37     private final ColorMatrix mMatrix = new ColorMatrix();
38     private final ColorMatrix mGrayscaleMatrix = new ColorMatrix();
39     private final long mFadeDuration;
40 
ViewInvertHelper(View target, long fadeDuration)41     public ViewInvertHelper(View target, long fadeDuration) {
42         mTarget = target;
43         mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(mTarget.getContext(),
44                 android.R.interpolator.linear_out_slow_in);
45         mFadeDuration = fadeDuration;
46     }
47 
fade(final boolean invert, long delay)48     public void fade(final boolean invert, long delay) {
49         float startIntensity = invert ? 0f : 1f;
50         float endIntensity = invert ? 1f : 0f;
51         ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
52         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
53             @Override
54             public void onAnimationUpdate(ValueAnimator animation) {
55                 updateInvertPaint((Float) animation.getAnimatedValue());
56                 mTarget.setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
57             }
58         });
59         animator.addListener(new AnimatorListenerAdapter() {
60             @Override
61             public void onAnimationEnd(Animator animation) {
62                 if (!invert) {
63                     mTarget.setLayerType(View.LAYER_TYPE_NONE, null);
64                 }
65             }
66         });
67         animator.setDuration(mFadeDuration);
68         animator.setInterpolator(mLinearOutSlowInInterpolator);
69         animator.setStartDelay(delay);
70         animator.start();
71     }
72 
update(boolean invert)73     public void update(boolean invert) {
74         if (invert) {
75             updateInvertPaint(1f);
76             mTarget.setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
77         } else {
78             mTarget.setLayerType(View.LAYER_TYPE_NONE, null);
79         }
80     }
81 
getTarget()82     public View getTarget() {
83         return mTarget;
84     }
85 
updateInvertPaint(float intensity)86     private void updateInvertPaint(float intensity) {
87         float components = 1 - 2 * intensity;
88         final float[] invert = {
89                 components, 0f,         0f,         0f, 255f * intensity,
90                 0f,         components, 0f,         0f, 255f * intensity,
91                 0f,         0f,         components, 0f, 255f * intensity,
92                 0f,         0f,         0f,         1f, 0f
93         };
94         mMatrix.set(invert);
95         mGrayscaleMatrix.setSaturation(1 - intensity);
96         mMatrix.preConcat(mGrayscaleMatrix);
97         mDarkPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix));
98     }
99 }