1 /* 2 * Copyright (C) 2012 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.launcher3; 18 19 import android.animation.Animator; 20 import android.animation.Animator.AnimatorListener; 21 import android.animation.AnimatorListenerAdapter; 22 import android.graphics.Color; 23 import android.graphics.drawable.ColorDrawable; 24 import android.graphics.drawable.Drawable; 25 import android.util.FloatProperty; 26 import android.util.IntProperty; 27 import android.view.View; 28 import android.view.ViewGroup.LayoutParams; 29 import android.widget.ImageView; 30 import android.widget.TextView; 31 32 import com.android.launcher3.util.MultiScalePropertyFactory; 33 34 public class LauncherAnimUtils { 35 /** 36 * Durations for various state animations. These are not defined in resources to allow 37 * easier access from static classes and enums 38 */ 39 public static final int SPRING_LOADED_EXIT_DELAY = 500; 40 41 // Progress after which the transition is assumed to be a success 42 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f; 43 public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f; 44 45 public static final IntProperty<Drawable> DRAWABLE_ALPHA = 46 new IntProperty<Drawable>("drawableAlpha") { 47 @Override 48 public Integer get(Drawable drawable) { 49 return drawable.getAlpha(); 50 } 51 52 @Override 53 public void setValue(Drawable drawable, int alpha) { 54 drawable.setAlpha(alpha); 55 } 56 }; 57 58 public static final FloatProperty<View> SCALE_PROPERTY = 59 new FloatProperty<View>("scale") { 60 @Override 61 public Float get(View view) { 62 return view.getScaleX(); 63 } 64 65 @Override 66 public void setValue(View view, float scale) { 67 view.setScaleX(scale); 68 view.setScaleY(scale); 69 } 70 }; 71 72 /** 73 * Property to set the scale of workspace. The value is based on a combination 74 * of all the ones set, to have a smooth experience even in the case of overlapping scaling 75 * animation. 76 */ 77 public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY = 78 new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property"); 79 80 /** Property to set the scale of hotseat. */ 81 public static final MultiScalePropertyFactory<Hotseat> HOTSEAT_SCALE_PROPERTY_FACTORY = 82 new MultiScalePropertyFactory<Hotseat>("hotseat_scale_property"); 83 84 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1; 85 public static final int SCALE_INDEX_WORKSPACE_STATE = 2; 86 public static final int SCALE_INDEX_REVEAL_ANIM = 3; 87 public static final int SCALE_INDEX_WIDGET_TRANSITION = 4; 88 89 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */ blockedFlingDurationFactor(float velocity)90 public static int blockedFlingDurationFactor(float velocity) { 91 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f); 92 } 93 94 public static final IntProperty<LayoutParams> LAYOUT_WIDTH = 95 new IntProperty<LayoutParams>("width") { 96 @Override 97 public Integer get(LayoutParams lp) { 98 return lp.width; 99 } 100 101 @Override 102 public void setValue(LayoutParams lp, int width) { 103 lp.width = width; 104 } 105 }; 106 107 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT = 108 new IntProperty<LayoutParams>("height") { 109 @Override 110 public Integer get(LayoutParams lp) { 111 return lp.height; 112 } 113 114 @Override 115 public void setValue(LayoutParams lp, int height) { 116 lp.height = height; 117 } 118 }; 119 120 public static final IntProperty<TextView> TEXT_COLOR = 121 new IntProperty<TextView>("textColor") { 122 @Override 123 public Integer get(TextView view) { 124 return view.getTextColors().getDefaultColor(); 125 } 126 127 @Override 128 public void setValue(TextView view, int color) { 129 view.setTextColor(color); 130 } 131 }; 132 133 public static final IntProperty<TextView> HINT_TEXT_COLOR = 134 new IntProperty<TextView>("hintTextColor") { 135 @Override 136 public Integer get(TextView view) { 137 return view.getHintTextColors().getDefaultColor(); 138 } 139 140 @Override 141 public void setValue(TextView view, int color) { 142 view.setHintTextColor(color); 143 } 144 }; 145 146 public static final FloatProperty<View> VIEW_TRANSLATE_X = 147 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X 148 : new FloatProperty<View>("translateX") { 149 @Override 150 public void setValue(View view, float v) { 151 view.setTranslationX(v); 152 } 153 154 @Override 155 public Float get(View view) { 156 return view.getTranslationX(); 157 } 158 }; 159 160 public static final FloatProperty<View> VIEW_TRANSLATE_Y = 161 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y 162 : new FloatProperty<View>("translateY") { 163 @Override 164 public void setValue(View view, float v) { 165 view.setTranslationY(v); 166 } 167 168 @Override 169 public Float get(View view) { 170 return view.getTranslationY(); 171 } 172 }; 173 174 public static final FloatProperty<View> VIEW_ALPHA = 175 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA 176 : new FloatProperty<View>("alpha") { 177 @Override 178 public void setValue(View view, float v) { 179 view.setAlpha(v); 180 } 181 182 @Override 183 public Float get(View view) { 184 return view.getAlpha(); 185 } 186 }; 187 188 public static final IntProperty<View> VIEW_BACKGROUND_COLOR = 189 new IntProperty<View>("backgroundColor") { 190 @Override 191 public void setValue(View view, int color) { 192 view.setBackgroundColor(color); 193 } 194 195 @Override 196 public Integer get(View view) { 197 if (!(view.getBackground() instanceof ColorDrawable)) { 198 return Color.TRANSPARENT; 199 } 200 return ((ColorDrawable) view.getBackground()).getColor(); 201 } 202 }; 203 204 public static final FloatProperty<ImageView> ROTATION_DRAWABLE_PERCENT = 205 new FloatProperty<ImageView>("drawableRotationPercent") { 206 // RotateDrawable linearly interpolates the rotation degrees between fromDegrees 207 // and toDegrees using the drawable level as a percent of its MAX_LEVEL. 208 private static final int MAX_LEVEL = 10000; 209 210 @Override 211 public void setValue(ImageView view, float percent) { 212 view.setImageLevel((int) (percent * MAX_LEVEL)); 213 } 214 215 @Override 216 public Float get(ImageView view) { 217 return view.getDrawable().getLevel() / (float) MAX_LEVEL; 218 } 219 }; 220 221 /** 222 * Utility method to create an {@link AnimatorListener} which executes a callback on animation 223 * cancel. Once the cancel has been dispatched, this listener will no longer be called. 224 */ newSingleUseCancelListener(Runnable callback)225 public static AnimatorListener newSingleUseCancelListener(Runnable callback) { 226 return newCancelListener(callback, true); 227 } 228 229 /** 230 * Utility method to create an {@link AnimatorListener} which executes a callback on animation 231 * cancel. 232 * 233 * @param isSingleUse {@code true} means the callback will be called at most once 234 */ newCancelListener(Runnable callback, boolean isSingleUse)235 public static AnimatorListener newCancelListener(Runnable callback, boolean isSingleUse) { 236 return new AnimatorListenerAdapter() { 237 boolean mDispatched = false; 238 239 @Override 240 public void onAnimationCancel(Animator animation) { 241 if (!mDispatched) { 242 if (isSingleUse) { 243 mDispatched = true; 244 } 245 callback.run(); 246 } 247 } 248 }; 249 } 250 251 /** 252 * A property that updates the specified property within a given range of values (ie. even if 253 * the animator goes beyond 0..1, the interpolated value will still be bounded). 254 * @param <T> the specified property 255 */ 256 public static class ClampedProperty<T> extends FloatProperty<T> { 257 private final FloatProperty<T> mProperty; 258 private final float mMinValue; 259 private final float mMaxValue; 260 ClampedProperty(FloatProperty<T> property, float minValue, float maxValue)261 public ClampedProperty(FloatProperty<T> property, float minValue, float maxValue) { 262 super(property.getName() + "Clamped"); 263 mProperty = property; 264 mMinValue = minValue; 265 mMaxValue = maxValue; 266 } 267 268 @Override setValue(T t, float v)269 public void setValue(T t, float v) { 270 mProperty.set(t, Utilities.boundToRange(v, mMinValue, mMaxValue)); 271 } 272 273 @Override get(T t)274 public Float get(T t) { 275 return mProperty.get(t); 276 } 277 } 278 } 279