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