1 package com.xtremelabs.robolectric.shadows; 2 3 import android.view.animation.Animation; 4 import android.view.animation.Interpolator; 5 import android.view.animation.ShadowAnimationBridge; 6 import android.view.animation.Transformation; 7 import com.xtremelabs.robolectric.internal.Implementation; 8 import com.xtremelabs.robolectric.internal.Implements; 9 import com.xtremelabs.robolectric.internal.RealObject; 10 11 /** 12 * Shadow implementation of {@code Animation} that provides support for invoking listener callbacks. 13 */ 14 @SuppressWarnings({"UnusedDeclaration"}) 15 @Implements(Animation.class) 16 public class ShadowAnimation { 17 18 private Animation.AnimationListener listener; 19 private Interpolator interpolator; 20 private boolean startFlag = false; 21 private long durationMillis = 0; 22 private int repeatCount; 23 private int repeatMode; 24 private long startOffset; 25 private int loadedFromResourceId = -1; 26 private boolean fillAfter; 27 28 @RealObject 29 private Animation realAnimation; 30 31 @Implementation setAnimationListener(Animation.AnimationListener l)32 public void setAnimationListener(Animation.AnimationListener l) { 33 listener = l; 34 } 35 36 @Implementation start()37 public void start() { 38 startFlag = true; 39 if (listener != null) { 40 listener.onAnimationStart(realAnimation); 41 } 42 } 43 44 @Implementation cancel()45 public void cancel() { 46 startFlag = false; 47 if (listener != null) { 48 listener.onAnimationEnd(realAnimation); 49 } 50 } 51 52 @Implementation hasStarted()53 public boolean hasStarted() { 54 return startFlag; 55 } 56 57 @Implementation setDuration(long durationMillis)58 public void setDuration(long durationMillis) { 59 this.durationMillis = durationMillis; 60 } 61 62 @Implementation getDuration()63 public long getDuration() { 64 return durationMillis; 65 } 66 67 @Implementation setInterpolator(Interpolator interpolator)68 public void setInterpolator(Interpolator interpolator) { 69 this.interpolator = interpolator; 70 } 71 72 @Implementation getInterpolator()73 public Interpolator getInterpolator() { 74 return interpolator; 75 } 76 77 @Implementation setRepeatCount(int repeatCount)78 public void setRepeatCount(int repeatCount) { 79 this.repeatCount = repeatCount; 80 } 81 82 @Implementation getRepeatCount()83 public int getRepeatCount() { 84 return repeatCount; 85 } 86 87 @Implementation setRepeatMode(int repeatMode)88 public void setRepeatMode(int repeatMode) { 89 this.repeatMode = repeatMode; 90 } 91 92 @Implementation getRepeatMode()93 public int getRepeatMode() { 94 return repeatMode; 95 } 96 97 @Implementation setStartOffset(long startOffset)98 public void setStartOffset(long startOffset) { 99 this.startOffset = startOffset; 100 } 101 102 @Implementation getStartOffset()103 public long getStartOffset() { 104 return startOffset; 105 } 106 107 @Implementation setFillAfter(boolean fillAfter)108 public void setFillAfter(boolean fillAfter) { 109 this.fillAfter = fillAfter; 110 } 111 112 @Implementation getFillAfter()113 public boolean getFillAfter() { 114 return fillAfter; 115 } 116 117 /** 118 * Non-Android accessor. Returns most recently set animation listener. 119 * 120 * @return 121 */ getAnimationListener()122 public Animation.AnimationListener getAnimationListener() { 123 return listener; 124 } 125 126 /** 127 * Non-Android accessor. Use to simulate repeat loops of animation. 128 */ invokeRepeat()129 public void invokeRepeat() { 130 if (listener != null) { 131 listener.onAnimationRepeat(realAnimation); 132 } 133 } 134 135 /** 136 * Non-Android accessor. Use to simulate end of animation. 137 */ invokeEnd()138 public void invokeEnd() { 139 if (listener != null) { 140 listener.onAnimationEnd(realAnimation); 141 } 142 new ShadowAnimationBridge(realAnimation).applyTransformation(1.0f, new Transformation()); 143 } 144 setLoadedFromResourceId(int loadedFromResourceId)145 public void setLoadedFromResourceId(int loadedFromResourceId) { 146 this.loadedFromResourceId = loadedFromResourceId; 147 } 148 getLoadedFromResourceId()149 public int getLoadedFromResourceId() { 150 if (loadedFromResourceId == -1) { 151 throw new IllegalStateException("not loaded from a resource"); 152 } 153 return loadedFromResourceId; 154 } 155 156 } 157