Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>/** * 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; } } </code></pre> <p>To set this animation to a view:</p> <pre><code> animation.setResetBlocked(true); view.setAnimation(animation); animation.setResetBlocked(false); </code></pre> <p>And to start an animation (previously set by setAnimation) TWO things must be done:</p> <pre><code> animation.start(0.0f, 1.0f, FADE_IN_DURATION); </code></pre> <p>And after that you must manually call invalidate() on every view that is affected by the animation.</p> <p>The usual startAnimation() does invalidate() for you, but setAnimation doesn't. (read the comment on View.setAnimation() method in android's sources).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload