StackStateAnimator.java revision 0dd6881ea481c855976214807c17595b34a2920a
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.statusbar.stack; 18 19import android.animation.ValueAnimator; 20import android.view.View; 21import android.view.animation.AnimationUtils; 22import android.view.animation.Interpolator; 23import com.android.systemui.statusbar.ExpandableView; 24 25import java.util.ArrayList; 26 27/** 28 * An stack state animator which handles animations to new StackScrollStates 29 */ 30public class StackStateAnimator { 31 32 private static final int ANIMATION_DURATION = 360; 33 34 private final Interpolator mFastOutSlowInInterpolator; 35 public NotificationStackScrollLayout mHostLayout; 36 private boolean mAnimationIsRunning; 37 private ArrayList<NotificationStackScrollLayout.AnimationEvent> mHandledEvents = 38 new ArrayList<>(); 39 40 public StackStateAnimator(NotificationStackScrollLayout hostLayout) { 41 mHostLayout = hostLayout; 42 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(hostLayout.getContext(), 43 android.R.interpolator.fast_out_slow_in); 44 } 45 46 public boolean isRunning() { 47 return mAnimationIsRunning; 48 } 49 50 public void startAnimationForEvents( 51 ArrayList<NotificationStackScrollLayout.AnimationEvent> mAnimationEvents, 52 StackScrollState finalState) { 53 int numEvents = mAnimationEvents.size(); 54 if (numEvents == 0) { 55 // No events, so we don't perform any animation 56 return; 57 } 58 long lastEventStartTime = mAnimationEvents.get(numEvents - 1).eventStartTime; 59 long eventEnd = lastEventStartTime + ANIMATION_DURATION; 60 long currentTime = AnimationUtils.currentAnimationTimeMillis(); 61 long newDuration = eventEnd - currentTime; 62 if (newDuration <= 0) { 63 // last event is long before this, so we don't do anything 64 return; 65 } 66 initializeAddedViewStates(mAnimationEvents, finalState); 67 int childCount = mHostLayout.getChildCount(); 68 boolean isFirstAnimatingView = true; 69 for (int i = 0; i < childCount; i++) { 70 final ExpandableView child = (ExpandableView) mHostLayout.getChildAt(i); 71 StackScrollState.ViewState viewState = finalState.getViewStateForView(child); 72 if (viewState == null) { 73 continue; 74 } 75 int childVisibility = child.getVisibility(); 76 boolean wasVisible = childVisibility == View.VISIBLE; 77 final float alpha = viewState.alpha; 78 if (!wasVisible && alpha != 0 && !viewState.gone) { 79 child.setVisibility(View.VISIBLE); 80 } 81 82 startPropertyAnimation(newDuration, isFirstAnimatingView, child, viewState, alpha); 83 84 // TODO: animate clipBounds 85 child.setClipBounds(null); 86 int currentHeigth = child.getActualHeight(); 87 if (viewState.height != currentHeigth) { 88 startHeightAnimation(newDuration, child, viewState, currentHeigth); 89 } 90 isFirstAnimatingView = false; 91 } 92 mAnimationIsRunning = true; 93 } 94 95 private void startPropertyAnimation(long newDuration, final boolean hasFinishAction, 96 final ExpandableView child, StackScrollState.ViewState viewState, final float alpha) { 97 child.animate().setInterpolator(mFastOutSlowInInterpolator) 98 .alpha(alpha) 99 .translationY(viewState.yTranslation) 100 .translationZ(viewState.zTranslation) 101 .setDuration(newDuration) 102 .withEndAction(new Runnable() { 103 @Override 104 public void run() { 105 mAnimationIsRunning = false; 106 if (hasFinishAction) { 107 mHandledEvents.clear(); 108 mHostLayout.onChildAnimationFinished(); 109 } 110 if (alpha == 0) { 111 child.setVisibility(View.INVISIBLE); 112 } 113 } 114 }); 115 } 116 117 private void startHeightAnimation(long newDuration, final ExpandableView child, 118 StackScrollState.ViewState viewState, int currentHeigth) { 119 ValueAnimator heightAnimator = ValueAnimator.ofInt(currentHeigth, viewState.height); 120 heightAnimator.setInterpolator(mFastOutSlowInInterpolator); 121 heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 122 @Override 123 public void onAnimationUpdate(ValueAnimator animation) { 124 child.setActualHeight((int) animation.getAnimatedValue()); 125 } 126 }); 127 heightAnimator.setDuration(newDuration); 128 heightAnimator.start(); 129 } 130 131 /** 132 * Initialize the viewStates for the added children 133 * 134 * @param animationEvents the animation events who contain the added children 135 * @param finalState the final state to animate to 136 */ 137 private void initializeAddedViewStates( 138 ArrayList<NotificationStackScrollLayout.AnimationEvent> animationEvents, 139 StackScrollState finalState) { 140 for (NotificationStackScrollLayout.AnimationEvent event: animationEvents) { 141 View changingView = event.changingView; 142 if (event.animationType == NotificationStackScrollLayout.AnimationEvent 143 .ANIMATION_TYPE_ADD && !mHandledEvents.contains(event)) { 144 145 // This item is added, initialize it's properties. 146 StackScrollState.ViewState viewState = finalState.getViewStateForView(changingView); 147 if (viewState == null) { 148 // The position for this child was never generated, let's continue. 149 continue; 150 } 151 changingView.setAlpha(0); 152 changingView.setTranslationY(viewState.yTranslation); 153 changingView.setTranslationZ(viewState.zTranslation); 154 mHandledEvents.add(event); 155 } 156 } 157 } 158} 159