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