|
| 1 | +/** |
| 2 | + * Copyright 2014 Zhenguo Jin (jinzhenguo1990@gmail.com) |
| 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 | +package com.worthed.util; |
| 17 | + |
| 18 | +import android.view.animation.AlphaAnimation; |
| 19 | +import android.view.animation.Animation; |
| 20 | +import android.view.animation.Animation.AnimationListener; |
| 21 | +import android.view.animation.RotateAnimation; |
| 22 | +import android.view.animation.ScaleAnimation; |
| 23 | + |
| 24 | +/** |
| 25 | + * 动画工具类 |
| 26 | + * @author zhenguo |
| 27 | + * |
| 28 | + */ |
| 29 | +public class AnimationUtils { |
| 30 | + |
| 31 | + /** |
| 32 | + * 默认动画持续时间 |
| 33 | + */ |
| 34 | + public static final long DEFAULT_ANIMATION_DURATION = 400; |
| 35 | + |
| 36 | + /** |
| 37 | + * 获取一个旋转动画 |
| 38 | + * @param fromDegrees 开始角度 |
| 39 | + * @param toDegrees 结束角度 |
| 40 | + * @param pivotXType 旋转中心点X轴坐标相对类型 |
| 41 | + * @param pivotXValue 旋转中心点X轴坐标 |
| 42 | + * @param pivotYType 旋转中心点Y轴坐标相对类型 |
| 43 | + * @param pivotYValue 旋转中心点Y轴坐标 |
| 44 | + * @param durationMillis 持续时间 |
| 45 | + * @param animationListener 动画监听器 |
| 46 | + * @return 一个旋转动画 |
| 47 | + */ |
| 48 | + public static RotateAnimation getRotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue, long durationMillis, AnimationListener animationListener){ |
| 49 | + RotateAnimation rotateAnimation = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue); |
| 50 | + rotateAnimation.setDuration(durationMillis); |
| 51 | + if(animationListener != null){ |
| 52 | + rotateAnimation.setAnimationListener(animationListener); |
| 53 | + } |
| 54 | + return rotateAnimation; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 获取一个根据视图自身中心点旋转的动画 |
| 59 | + * @param durationMillis 动画持续时间 |
| 60 | + * @param animationListener 动画监听器 |
| 61 | + * @return 一个根据中心点旋转的动画 |
| 62 | + */ |
| 63 | + public static RotateAnimation getRotateAnimationByCenter(long durationMillis, AnimationListener animationListener){ |
| 64 | + return getRotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, durationMillis, animationListener); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 获取一个根据中心点旋转的动画 |
| 69 | + * @param duration 动画持续时间 |
| 70 | + * @return 一个根据中心点旋转的动画 |
| 71 | + */ |
| 72 | + public static RotateAnimation getRotateAnimationByCenter(long duration){ |
| 73 | + return getRotateAnimationByCenter(duration, null); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 获取一个根据视图自身中心点旋转的动画 |
| 78 | + * @param durationMillis 动画持续时间 |
| 79 | + * @param animationListener 动画监听器 |
| 80 | + * @return 一个根据中心点旋转的动画 |
| 81 | + */ |
| 82 | + public static RotateAnimation getRotateAnimationByCenter( AnimationListener animationListener){ |
| 83 | + return getRotateAnimationByCenter(DEFAULT_ANIMATION_DURATION, animationListener); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 获取一个根据中心点旋转的动画 |
| 88 | + * @return 一个根据中心点旋转的动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 89 | + */ |
| 90 | + public static RotateAnimation getRotateAnimationByCenter(){ |
| 91 | + return getRotateAnimationByCenter(DEFAULT_ANIMATION_DURATION, null); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 获取一个透明度渐变动画 |
| 96 | + * @param fromAlpha 开始时的透明度 |
| 97 | + * @param toAlpha 结束时的透明度都 |
| 98 | + * @param durationMillis 持续时间 |
| 99 | + * @param animationListener 动画监听器 |
| 100 | + * @return 一个透明度渐变动画 |
| 101 | + */ |
| 102 | + public static AlphaAnimation getAlphaAnimation(float fromAlpha, float toAlpha, long durationMillis, AnimationListener animationListener){ |
| 103 | + AlphaAnimation alphaAnimation = new AlphaAnimation(fromAlpha, toAlpha); |
| 104 | + alphaAnimation.setDuration(durationMillis); |
| 105 | + if(animationListener != null){ |
| 106 | + alphaAnimation.setAnimationListener(animationListener); |
| 107 | + } |
| 108 | + return alphaAnimation; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * 获取一个透明度渐变动画 |
| 113 | + * @param fromAlpha 开始时的透明度 |
| 114 | + * @param toAlpha 结束时的透明度都 |
| 115 | + * @param durationMillis 持续时间 |
| 116 | + * @return 一个透明度渐变动画 |
| 117 | + */ |
| 118 | + public static AlphaAnimation getAlphaAnimation(float fromAlpha, float toAlpha, long durationMillis){ |
| 119 | + return getAlphaAnimation(fromAlpha, toAlpha, durationMillis, null); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * 获取一个透明度渐变动画 |
| 124 | + * @param fromAlpha 开始时的透明度 |
| 125 | + * @param toAlpha 结束时的透明度都 |
| 126 | + * @param animationListener 动画监听器 |
| 127 | + * @return 一个透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 128 | + */ |
| 129 | + public static AlphaAnimation getAlphaAnimation(float fromAlpha, float toAlpha, AnimationListener animationListener){ |
| 130 | + return getAlphaAnimation(fromAlpha, toAlpha, DEFAULT_ANIMATION_DURATION, animationListener); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 获取一个透明度渐变动画 |
| 135 | + * @param fromAlpha 开始时的透明度 |
| 136 | + * @param toAlpha 结束时的透明度都 |
| 137 | + * @return 一个透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 138 | + */ |
| 139 | + public static AlphaAnimation getAlphaAnimation(float fromAlpha, float toAlpha){ |
| 140 | + return getAlphaAnimation(fromAlpha, toAlpha, DEFAULT_ANIMATION_DURATION, null); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * 获取一个由完全显示变为不可见的透明度渐变动画 |
| 145 | + * @param durationMillis 持续时间 |
| 146 | + * @param animationListener 动画监听器 |
| 147 | + * @return 一个由完全显示变为不可见的透明度渐变动画 |
| 148 | + */ |
| 149 | + public static AlphaAnimation getHiddenAlphaAnimation(long durationMillis, AnimationListener animationListener){ |
| 150 | + return getAlphaAnimation(1.0f, 0.0f, durationMillis, animationListener); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * 获取一个由完全显示变为不可见的透明度渐变动画 |
| 155 | + * @param durationMillis 持续时间 |
| 156 | + * @return 一个由完全显示变为不可见的透明度渐变动画 |
| 157 | + */ |
| 158 | + public static AlphaAnimation getHiddenAlphaAnimation(long durationMillis){ |
| 159 | + return getHiddenAlphaAnimation(durationMillis, null); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * 获取一个由完全显示变为不可见的透明度渐变动画 |
| 164 | + * @param animationListener 动画监听器 |
| 165 | + * @return 一个由完全显示变为不可见的透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 166 | + */ |
| 167 | + public static AlphaAnimation getHiddenAlphaAnimation(AnimationListener animationListener){ |
| 168 | + return getHiddenAlphaAnimation(DEFAULT_ANIMATION_DURATION, animationListener); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * 获取一个由完全显示变为不可见的透明度渐变动画 |
| 173 | + * @return 一个由完全显示变为不可见的透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 174 | + */ |
| 175 | + public static AlphaAnimation getHiddenAlphaAnimation(){ |
| 176 | + return getHiddenAlphaAnimation(DEFAULT_ANIMATION_DURATION, null); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * 获取一个由不可见变为完全显示的透明度渐变动画 |
| 181 | + * @param durationMillis 持续时间 |
| 182 | + * @param animationListener 动画监听器 |
| 183 | + * @return 一个由不可见变为完全显示的透明度渐变动画 |
| 184 | + */ |
| 185 | + public static AlphaAnimation getShowAlphaAnimation(long durationMillis, AnimationListener animationListener){ |
| 186 | + return getAlphaAnimation(0.0f, 1.0f, durationMillis, animationListener); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * 获取一个由不可见变为完全显示的透明度渐变动画 |
| 191 | + * @param durationMillis 持续时间 |
| 192 | + * @return 一个由不可见变为完全显示的透明度渐变动画 |
| 193 | + */ |
| 194 | + public static AlphaAnimation getShowAlphaAnimation(long durationMillis){ |
| 195 | + return getAlphaAnimation(0.0f, 1.0f, durationMillis, null); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * 获取一个由不可见变为完全显示的透明度渐变动画 |
| 200 | + * @param animationListener 动画监听器 |
| 201 | + * @return 一个由不可见变为完全显示的透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 202 | + */ |
| 203 | + public static AlphaAnimation getShowAlphaAnimation(AnimationListener animationListener){ |
| 204 | + return getAlphaAnimation(0.0f, 1.0f, DEFAULT_ANIMATION_DURATION, animationListener); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * 获取一个由不可见变为完全显示的透明度渐变动画 |
| 209 | + * @return 一个由不可见变为完全显示的透明度渐变动画,默认持续时间为DEFAULT_ANIMATION_DURATION |
| 210 | + */ |
| 211 | + public static AlphaAnimation getShowAlphaAnimation(){ |
| 212 | + return getAlphaAnimation(0.0f, 1.0f, DEFAULT_ANIMATION_DURATION, null); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * 获取一个缩小动画 |
| 217 | + * @param durationMillis |
| 218 | + * @param animationListener |
| 219 | + * @return |
| 220 | + */ |
| 221 | + public static ScaleAnimation getLessenScaleAnimation(long durationMillis, AnimationListener animationListener){ |
| 222 | + ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, ScaleAnimation.RELATIVE_TO_SELF, ScaleAnimation.RELATIVE_TO_SELF); |
| 223 | + scaleAnimation.setDuration(durationMillis); |
| 224 | + scaleAnimation.setAnimationListener(animationListener); |
| 225 | + return scaleAnimation; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * 获取一个缩小动画 |
| 230 | + * @param durationMillis |
| 231 | + * @return |
| 232 | + */ |
| 233 | + public static ScaleAnimation getLessenScaleAnimation(long durationMillis){ |
| 234 | + return getLessenScaleAnimation(DEFAULT_ANIMATION_DURATION); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * 获取一个缩小动画 |
| 239 | + * @param animationListener |
| 240 | + * @return |
| 241 | + */ |
| 242 | + public static ScaleAnimation getLessenScaleAnimation(AnimationListener animationListener){ |
| 243 | + return getLessenScaleAnimation(DEFAULT_ANIMATION_DURATION, null); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * 获取一个放大动画 |
| 248 | + * @param durationMillis |
| 249 | + * @param animationListener |
| 250 | + * @return |
| 251 | + */ |
| 252 | + public static ScaleAnimation getAmplificationAnimation(long durationMillis, AnimationListener animationListener){ |
| 253 | + ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, ScaleAnimation.RELATIVE_TO_SELF); |
| 254 | + scaleAnimation.setDuration(durationMillis); |
| 255 | + scaleAnimation.setAnimationListener(animationListener); |
| 256 | + return scaleAnimation; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * 获取一个放大动画 |
| 261 | + * @param durationMillis |
| 262 | + * @return |
| 263 | + */ |
| 264 | + public static ScaleAnimation getAmplificationAnimation(long durationMillis){ |
| 265 | + return getLessenScaleAnimation(DEFAULT_ANIMATION_DURATION); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * 获取一个放大动画 |
| 270 | + * @param animationListener |
| 271 | + * @return |
| 272 | + */ |
| 273 | + public static ScaleAnimation getAmplificationAnimation(AnimationListener animationListener){ |
| 274 | + return getLessenScaleAnimation(DEFAULT_ANIMATION_DURATION, null); |
| 275 | + } |
| 276 | + |
| 277 | +} |
0 commit comments