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 30 public class LauncherAnimUtils { 31 /** 32 * Durations for various state animations. These are not defined in resources to allow 33 * easier access from static classes and enums 34 */ 35 public static final int SPRING_LOADED_EXIT_DELAY = 500; 36 37 // Progress after which the transition is assumed to be a success 38 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f; 39 40 public static final IntProperty<Drawable> DRAWABLE_ALPHA = 41 new IntProperty<Drawable>("drawableAlpha") { 42 @Override 43 public Integer get(Drawable drawable) { 44 return drawable.getAlpha(); 45 } 46 47 @Override 48 public void setValue(Drawable drawable, int alpha) { 49 drawable.setAlpha(alpha); 50 } 51 }; 52 53 public static final FloatProperty<View> SCALE_PROPERTY = 54 new FloatProperty<View>("scale") { 55 @Override 56 public Float get(View view) { 57 return view.getScaleX(); 58 } 59 60 @Override 61 public void setValue(View view, float scale) { 62 view.setScaleX(scale); 63 view.setScaleY(scale); 64 } 65 }; 66 67 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */ blockedFlingDurationFactor(float velocity)68 public static int blockedFlingDurationFactor(float velocity) { 69 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f); 70 } 71 72 public static final IntProperty<LayoutParams> LAYOUT_WIDTH = 73 new IntProperty<LayoutParams>("width") { 74 @Override 75 public Integer get(LayoutParams lp) { 76 return lp.width; 77 } 78 79 @Override 80 public void setValue(LayoutParams lp, int width) { 81 lp.width = width; 82 } 83 }; 84 85 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT = 86 new IntProperty<LayoutParams>("height") { 87 @Override 88 public Integer get(LayoutParams lp) { 89 return lp.height; 90 } 91 92 @Override 93 public void setValue(LayoutParams lp, int height) { 94 lp.height = height; 95 } 96 }; 97 98 public static final FloatProperty<View> VIEW_TRANSLATE_X = 99 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X 100 : new FloatProperty<View>("translateX") { 101 @Override 102 public void setValue(View view, float v) { 103 view.setTranslationX(v); 104 } 105 106 @Override 107 public Float get(View view) { 108 return view.getTranslationX(); 109 } 110 }; 111 112 public static final FloatProperty<View> VIEW_TRANSLATE_Y = 113 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y 114 : new FloatProperty<View>("translateY") { 115 @Override 116 public void setValue(View view, float v) { 117 view.setTranslationY(v); 118 } 119 120 @Override 121 public Float get(View view) { 122 return view.getTranslationY(); 123 } 124 }; 125 126 public static final FloatProperty<View> VIEW_ALPHA = 127 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA 128 : new FloatProperty<View>("alpha") { 129 @Override 130 public void setValue(View view, float v) { 131 view.setAlpha(v); 132 } 133 134 @Override 135 public Float get(View view) { 136 return view.getAlpha(); 137 } 138 }; 139 140 public static final IntProperty<View> VIEW_BACKGROUND_COLOR = 141 new IntProperty<View>("backgroundColor") { 142 @Override 143 public void setValue(View view, int color) { 144 view.setBackgroundColor(color); 145 } 146 147 @Override 148 public Integer get(View view) { 149 if (!(view.getBackground() instanceof ColorDrawable)) { 150 return Color.TRANSPARENT; 151 } 152 return ((ColorDrawable) view.getBackground()).getColor(); 153 } 154 }; 155 156 /** 157 * Utility method to create an {@link AnimatorListener} which executes a callback on animation 158 * cancel. 159 */ newCancelListener(Runnable callback)160 public static AnimatorListener newCancelListener(Runnable callback) { 161 return new AnimatorListenerAdapter() { 162 163 boolean mDispatched = false; 164 165 @Override 166 public void onAnimationCancel(Animator animation) { 167 if (!mDispatched) { 168 mDispatched = true; 169 callback.run(); 170 } 171 } 172 }; 173 } 174 } 175