1 30 31 package com.jgoodies.animation; 32 33 import java.util.Arrays ; 34 import java.util.Collections ; 35 import java.util.Iterator ; 36 import java.util.LinkedList ; 37 import java.util.List ; 38 39 46 public final class Animations { 47 48 private Animations() { 49 } 51 52 53 62 public static Animation offset(long beginTime, Animation animation) { 63 return new OffsetAnimation(beginTime, animation); 64 } 65 66 67 74 public static Animation parallel(List animations) { 75 return new ParallelAnimation(animations); 76 } 77 78 79 88 public static Animation parallel(Animation animation1, 89 Animation animation2) { 90 List list = new LinkedList (); 91 list.add(animation1); 92 list.add(animation2); 93 return parallel(list); 94 } 95 96 97 105 public static Animation pause(long duration) { 106 return new PauseAnimation(duration); 107 } 108 109 110 119 public static Animation repeat(float repeatCount, Animation animation) { 120 long duration = (long) (animation.duration() * repeatCount); 121 return new RepeatedAnimation(duration, animation); 122 } 123 124 125 132 public static Animation reverse(Animation animation) { 133 return new ReversedAnimation(animation); 134 } 135 136 137 144 public static Animation sequential(List animations) { 145 return new SequencedAnimation(animations); 146 } 147 148 149 156 public static Animation sequential(Animation[] animations) { 157 return sequential(Arrays.asList(animations)); 158 } 159 160 161 169 public static Animation sequential(Animation first, Animation second) { 170 List sequence = new LinkedList (); 171 sequence.add(first); 172 sequence.add(second); 173 return sequential(sequence); 174 } 175 176 177 179 private static class OffsetAnimation extends AbstractAnimation { 181 private final Animation animation; 182 private final long beginTime; 183 184 private OffsetAnimation(long beginTime, Animation animation) { 185 super(beginTime + animation.duration(), true); 186 this.animation = animation; 187 this.beginTime = beginTime; 188 } 189 190 protected void applyEffect(long time) { 191 long relativeTime = time - beginTime; 192 if (relativeTime >= 0) 193 animation.animate(relativeTime); 194 } 195 196 } 197 198 201 public static abstract class OneTimeAnimation extends AbstractAnimation { 202 203 private boolean effectApplied; 204 205 208 public OneTimeAnimation() { 209 super(0, true); 210 effectApplied = false; 211 } 212 213 219 public void animate(long time) { 220 if (effectApplied) 221 return; 222 223 fireAnimationStarted(time); 224 applyEffect(time); 225 fireAnimationStopped(time); 226 effectApplied = true; 227 } 228 } 229 230 private static class ParallelAnimation extends AbstractAnimation { 232 private final List animations; 233 234 private ParallelAnimation(List animations) { 235 super(maxDuration(animations), true); 236 this.animations = animations; 237 } 238 239 private static long maxDuration(List animations) { 240 long maxDuration = 0; 241 for (Iterator i = animations.iterator(); i.hasNext();) { 242 Animation animation = (Animation) i.next(); 243 long duration = animation.duration(); 244 if (duration > maxDuration) 245 maxDuration = duration; 246 } 247 return maxDuration; 248 } 249 250 protected void applyEffect(long time) { 251 for (Iterator i = animations.iterator(); i.hasNext();) { 252 Animation animation = (Animation) i.next(); 253 animation.animate(time); 254 } 255 } 256 257 } 258 259 private static class PauseAnimation extends AbstractAnimation { 261 PauseAnimation(long duration) { 262 super(duration, true); 263 } 264 265 protected void applyEffect(long time) { 266 } 268 } 269 270 private static class RepeatedAnimation extends AbstractAnimation { 272 private final Animation animation; 273 private final long simpleDuration; 274 275 private RepeatedAnimation(long duration, Animation animation) { 276 super(duration, true); 277 this.animation = animation; 278 this.simpleDuration = animation.duration(); 279 } 280 281 protected void applyEffect(long time) { 282 animation.animate(time % simpleDuration); 283 } 284 } 285 286 private static class ReversedAnimation extends AbstractAnimation { 288 private final Animation animation; 289 290 private ReversedAnimation(Animation animation) { 291 super(animation.duration(), true); 292 this.animation = animation; 293 } 294 295 protected void applyEffect(long time) { 296 long reversedTime = duration() - time; 297 if (reversedTime < 0) 298 throw new IllegalArgumentException ("The time is outside the valid time interval."); 299 300 animation.animate(reversedTime); 301 } 302 } 303 304 private static class SequencedAnimation extends AbstractAnimation { 306 private final List animations; 307 308 private SequencedAnimation(List animations) { 309 super(cumulatedDuration(animations), true); 310 this.animations = Collections.unmodifiableList(animations); 311 if (this.animations.isEmpty()) 312 throw new IllegalArgumentException ("The list of animations must not be empty."); 313 } 314 315 private static long cumulatedDuration(List animations) { 316 long cumulatedDuration = 0; 317 for (Iterator i = animations.iterator(); i.hasNext();) { 318 Animation animation = (Animation) i.next(); 319 cumulatedDuration += animation.duration(); 320 } 321 return cumulatedDuration; 322 } 323 324 protected void applyEffect(long time) { 325 long startTime = 0; 326 for (Iterator i = animations.iterator(); i.hasNext();) { 327 Animation animation = (Animation) i.next(); 328 long relativeTime = time - startTime; 329 if (relativeTime > 0) 330 animation.animate(relativeTime); 331 startTime += animation.duration(); 332 } 333 } 334 335 } 336 337 } | Popular Tags |