Home | History | Annotate | Download | only in statusbar
      1 /*
      2  * Copyright (C) 2016 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.statusbar;
     18 
     19 import android.view.View;
     20 
     21 import com.android.systemui.Interpolators;
     22 import com.android.systemui.statusbar.stack.StackStateAnimator;
     23 
     24 /**
     25  * A helper to fade views in and out.
     26  */
     27 public class CrossFadeHelper {
     28     public static final long ANIMATION_DURATION_LENGTH = 210;
     29 
     30     public static void fadeOut(final View view, final Runnable endRunnable) {
     31         view.animate().cancel();
     32         view.animate()
     33                 .alpha(0f)
     34                 .setDuration(ANIMATION_DURATION_LENGTH)
     35                 .setInterpolator(Interpolators.ALPHA_OUT)
     36                 .withEndAction(new Runnable() {
     37                     @Override
     38                     public void run() {
     39                         if (endRunnable != null) {
     40                             endRunnable.run();
     41                         }
     42                         view.setVisibility(View.INVISIBLE);
     43                     }
     44                 });
     45         if (view.hasOverlappingRendering()) {
     46             view.animate().withLayer();
     47         }
     48     }
     49 
     50     public static void fadeOut(View view, float fadeOutAmount) {
     51         view.animate().cancel();
     52         if (fadeOutAmount == 1.0f) {
     53             view.setVisibility(View.INVISIBLE);
     54         } else if (view.getVisibility() == View.INVISIBLE) {
     55             view.setVisibility(View.VISIBLE);
     56         }
     57         fadeOutAmount = mapToFadeDuration(fadeOutAmount);
     58         float alpha = Interpolators.ALPHA_OUT.getInterpolation(1.0f - fadeOutAmount);
     59         view.setAlpha(alpha);
     60         updateLayerType(view, alpha);
     61     }
     62 
     63     private static float mapToFadeDuration(float fadeOutAmount) {
     64         // Assuming a linear interpolator, we can easily map it to our new duration
     65         float endPoint = (float) ANIMATION_DURATION_LENGTH
     66                 / (float) StackStateAnimator.ANIMATION_DURATION_STANDARD;
     67         return Math.min(fadeOutAmount / endPoint, 1.0f);
     68     }
     69 
     70     private static void updateLayerType(View view, float alpha) {
     71         if (view.hasOverlappingRendering() && alpha > 0.0f && alpha < 1.0f) {
     72             view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
     73         } else if (view.getLayerType() == View.LAYER_TYPE_HARDWARE) {
     74             view.setLayerType(View.LAYER_TYPE_NONE, null);
     75         }
     76     }
     77 
     78     public static void fadeIn(final View view) {
     79         view.animate().cancel();
     80         if (view.getVisibility() == View.INVISIBLE) {
     81             view.setAlpha(0.0f);
     82             view.setVisibility(View.VISIBLE);
     83         }
     84         view.animate()
     85                 .alpha(1f)
     86                 .setDuration(ANIMATION_DURATION_LENGTH)
     87                 .setInterpolator(Interpolators.ALPHA_IN)
     88                 .withEndAction(null);
     89         if (view.hasOverlappingRendering()) {
     90             view.animate().withLayer();
     91         }
     92     }
     93 
     94     public static void fadeIn(View view, float fadeInAmount) {
     95         view.animate().cancel();
     96         if (view.getVisibility() == View.INVISIBLE) {
     97             view.setVisibility(View.VISIBLE);
     98         }
     99         fadeInAmount = mapToFadeDuration(fadeInAmount);
    100         float alpha = Interpolators.ALPHA_IN.getInterpolation(fadeInAmount);
    101         view.setAlpha(alpha);
    102         updateLayerType(view, alpha);
    103     }
    104 }
    105