I've managed to share a single Animation instance among several Views. At least with an AlphaAnimation. I had a ListView and an animation that should be applied to a particular child of all list item views. In my case it should have been possible for the views to 'join' and 'leave' the shared animation at any time and it should not affect other animated views in any way or interfere with already running animation.
To achieve this I had to make an adjusted copy of android's stock AlphaAnimation.
My use case is rather special, but let it be here just in case someone has to deal with similar goal with ListView.
/**
* This class is a copy of android's stock AlphaAnimation with two adjustments:
* - fromAlpha, toAlpha and duration are settable at any time.
* - reset() method can be blocked. Reason: view.setAnimation() calls animation's reset() method
* which is not intended in our use case.
* For every new list item view we call setAnimation once for it's life time
* and animation should not be reset because it is shared by all list item views and may be in progress.
*/
public class MutableAlphaAnimation extends Animation {
private float mFromAlpha;
private float mToAlpha;
private boolean resetBlocked;
public MutableAlphaAnimation() {
}
public void start(float fromAlpha, float toAlpha, long duration) {
mFromAlpha = fromAlpha;
mToAlpha = toAlpha;
setDuration(duration);
setStartTime(START_ON_FIRST_FRAME);
}
public void setResetBlocked(boolean resetBlocked) {
this.resetBlocked = resetBlocked;
}
@Override
public void reset() {
if (! resetBlocked) super.reset();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float alpha = mFromAlpha;
t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));
}
@Override
public boolean willChangeTransformationMatrix() {
return false;
}
@Override
public boolean willChangeBounds() {
return false;
}
}
To set this animation to a view:
animation.setResetBlocked(true);
view.setAnimation(animation);
animation.setResetBlocked(false);
And to start an animation (previously set by setAnimation) TWO things must be done:
animation.start(0.0f, 1.0f, FADE_IN_DURATION);
And after that you must manually call invalidate() on every view that is affected by the animation.
The usual startAnimation() does invalidate() for you, but setAnimation doesn't.
(read the comment on View.setAnimation() method in android's sources).