ViewInvertHelper.java revision 9c7712d45ccec56a80873a83e56a83f810624b43
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;
27import android.view.animation.AnimationUtils;
28import android.view.animation.Interpolator;
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 Interpolator mLinearOutSlowInInterpolator;
39    private final ColorMatrix mMatrix = new ColorMatrix();
40    private final ColorMatrix mGrayscaleMatrix = new ColorMatrix();
41    private final long mFadeDuration;
42    private final ArrayList<View> mTargets = new ArrayList<>();
43
44    public ViewInvertHelper(View v, long fadeDuration) {
45        this(v.getContext(), fadeDuration);
46        addTarget(v);
47    }
48    public ViewInvertHelper(Context context, long fadeDuration) {
49        mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
50                android.R.interpolator.linear_out_slow_in);
51        mFadeDuration = fadeDuration;
52    }
53
54    private static ArrayList<View> constructArray(View target) {
55        final ArrayList<View> views = new ArrayList<>();
56        views.add(target);
57        return views;
58    }
59
60    public void clearTargets() {
61        mTargets.clear();
62    }
63
64    public void addTarget(View target) {
65        mTargets.add(target);
66    }
67
68    public void fade(final boolean invert, long delay) {
69        float startIntensity = invert ? 0f : 1f;
70        float endIntensity = invert ? 1f : 0f;
71        ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
72        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
73            @Override
74            public void onAnimationUpdate(ValueAnimator animation) {
75                updateInvertPaint((Float) animation.getAnimatedValue());
76                for (int i = 0; i < mTargets.size(); i++) {
77                    mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
78                }
79            }
80        });
81        animator.addListener(new AnimatorListenerAdapter() {
82            @Override
83            public void onAnimationEnd(Animator animation) {
84                if (!invert) {
85                    for (int i = 0; i < mTargets.size(); i++) {
86                        mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null);
87                    }
88                }
89            }
90        });
91        animator.setDuration(mFadeDuration);
92        animator.setInterpolator(mLinearOutSlowInInterpolator);
93        animator.setStartDelay(delay);
94        animator.start();
95    }
96
97    public void update(boolean invert) {
98        if (invert) {
99            updateInvertPaint(1f);
100            for (int i = 0; i < mTargets.size(); i++) {
101                mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
102            }
103        } else {
104            for (int i = 0; i < mTargets.size(); i++) {
105                mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null);
106            }
107        }
108    }
109
110    private void updateInvertPaint(float intensity) {
111        float components = 1 - 2 * intensity;
112        final float[] invert = {
113                components, 0f,         0f,         0f, 255f * intensity,
114                0f,         components, 0f,         0f, 255f * intensity,
115                0f,         0f,         components, 0f, 255f * intensity,
116                0f,         0f,         0f,         1f, 0f
117        };
118        mMatrix.set(invert);
119        mGrayscaleMatrix.setSaturation(1 - intensity);
120        mMatrix.preConcat(mGrayscaleMatrix);
121        mDarkPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix));
122    }
123
124    public void setInverted(boolean invert, boolean fade, long delay) {
125        if (fade) {
126            fade(invert, delay);
127        } else {
128            update(invert);
129        }
130    }
131}