1 /* 2 * Copyright (C) 2015 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.documentsui.dirlist; 18 19 import android.animation.Animator; 20 import android.animation.ArgbEvaluator; 21 import android.animation.ValueAnimator; 22 import android.content.Context; 23 import android.support.v4.util.ArrayMap; 24 import android.support.v7.widget.DefaultItemAnimator; 25 import android.support.v7.widget.RecyclerView; 26 import android.util.TypedValue; 27 28 import com.android.documentsui.R; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Map; 33 34 /** 35 * Performs change animations on Items in DirectoryFragment's RecyclerView. This class overrides 36 * the way selection animations are normally performed - instead of cross fading the old Item with a 37 * new Item, this class manually animates a background color change. This enables selected Items to 38 * correctly maintain focus. 39 */ 40 class DirectoryItemAnimator extends DefaultItemAnimator { 41 private final List<ColorAnimation> mPendingAnimations = new ArrayList<>(); 42 private final Map<RecyclerView.ViewHolder, ColorAnimation> mRunningAnimations = 43 new ArrayMap<>(); 44 private final Integer mDefaultColor; 45 private final Integer mSelectedColor; 46 47 public DirectoryItemAnimator(Context context) { 48 mDefaultColor = context.getResources().getColor(R.color.item_doc_background); 49 mSelectedColor = context.getResources().getColor(R.color.item_doc_background_selected); 50 } 51 52 @Override 53 public void runPendingAnimations() { 54 super.runPendingAnimations(); 55 for (ColorAnimation anim: mPendingAnimations) { 56 anim.start(); 57 mRunningAnimations.put(anim.viewHolder, anim); 58 } 59 mPendingAnimations.clear(); 60 } 61 62 @Override 63 public void endAnimation(RecyclerView.ViewHolder vh) { 64 super.endAnimation(vh); 65 66 for (int i = mPendingAnimations.size() - 1; i >= 0; --i) { 67 ColorAnimation anim = mPendingAnimations.get(i); 68 if (anim.viewHolder == vh) { 69 mPendingAnimations.remove(i); 70 anim.end(); 71 } 72 } 73 74 ColorAnimation anim = mRunningAnimations.get(vh); 75 if (anim != null) { 76 anim.cancel(); 77 } 78 } 79 80 @Override 81 public ItemHolderInfo recordPreLayoutInformation( 82 RecyclerView.State state, 83 RecyclerView.ViewHolder viewHolder, 84 @AdapterChanges int changeFlags, 85 List<Object> payloads) { 86 ItemInfo info = (ItemInfo) super.recordPreLayoutInformation(state, 87 viewHolder, changeFlags, payloads); 88 info.isActivated = viewHolder.itemView.isActivated(); 89 return info; 90 } 91 92 93 @Override 94 public ItemHolderInfo recordPostLayoutInformation( 95 RecyclerView.State state, RecyclerView.ViewHolder viewHolder) { 96 ItemInfo info = (ItemInfo) super.recordPostLayoutInformation(state, 97 viewHolder); 98 info.isActivated = viewHolder.itemView.isActivated(); 99 return info; 100 } 101 102 @Override 103 public ItemHolderInfo obtainHolderInfo() { 104 return new ItemInfo(); 105 } 106 107 @Override 108 public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder vh) { 109 return true; 110 } 111 112 class ItemInfo extends DefaultItemAnimator.ItemHolderInfo { 113 boolean isActivated; 114 }; 115 116 /** 117 * Animates changes in background color. 118 */ 119 class ColorAnimation 120 implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener { 121 ValueAnimator mValueAnimator; 122 final RecyclerView.ViewHolder viewHolder; 123 int mEndColor; 124 125 public ColorAnimation(RecyclerView.ViewHolder vh, int startColor, int endColor) 126 { 127 viewHolder = vh; 128 mValueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor); 129 mValueAnimator.addUpdateListener(this); 130 mValueAnimator.addListener(this); 131 132 mEndColor = endColor; 133 } 134 135 public void start() { 136 mValueAnimator.start(); 137 } 138 139 public void cancel() { 140 mValueAnimator.cancel(); 141 } 142 143 public void end() { 144 mValueAnimator.end(); 145 } 146 147 @Override 148 public void onAnimationUpdate(ValueAnimator animator) { 149 viewHolder.itemView.setBackgroundColor((Integer)animator.getAnimatedValue()); 150 } 151 152 @Override 153 public void onAnimationEnd(Animator animator) { 154 viewHolder.itemView.setBackgroundColor(mEndColor); 155 mRunningAnimations.remove(viewHolder); 156 dispatchAnimationFinished(viewHolder); 157 } 158 159 @Override 160 public void onAnimationStart(Animator animation) { 161 dispatchAnimationStarted(viewHolder); 162 } 163 164 @Override 165 public void onAnimationCancel(Animator animation) {} 166 167 @Override 168 public void onAnimationRepeat(Animator animation) {} 169 }; 170 }; 171