Skip to content Skip to sidebar Skip to footer

Applying Successive Animations To Imageview In Android

I would like to apply successive animations (say ScaleAnimation) to an ImageView showing a resource image. The animation is triggered by a button. For example, I would like to incr

Solution 1:

It seems as if the ImageView resets its state and the animation is always the same, instead of starting from the final state of the previous animation.

Precisely! I'm sure there's a use for fillAfter="true", but I haven't figured out the point for it yet.

What you need to do is set up an AnimationListener on each Animation of relevance, and do something in the listener's onAnimationEnd() to actually persist the end state of your animation. I haven't played with ScaleAnimation so I'm not quite sure what the way to "persist the end state" would be. If this were an AlphaAnimation, going from 1.0 to 0.0, you would make the widget INVISIBLE or GONE in onAnimationEnd(), for example.

Solution 2:

I've had the same problem and created the following code to easily use different animations. It only supports translation and alpha levels for now as I haven't used scaling, but could easily be extended to support more features.

I reset the scroll and the visibility before starting the animation, but that's just because I needed on/off animations.

And the "doEnd" boolean is there to avoid a stack overflow on the recursion (scrollTo calls onAnimationEnd for some obscure reason...)

privatevoidsetViewPos(View view, Animation anim, long time){
    // Get the transformationTransformationtrans=newTransformation();
    anim.getTransformation(time, trans);

    // Get the matrix valuesfloat[] values = newfloat[9];
    Matrixm= trans.getMatrix();
    m.getValues(values);

    // Get the position and apply the scrollfinalfloatx= values[Matrix.MTRANS_X];
    finalfloaty= values[Matrix.MTRANS_Y];
    view.scrollTo(-(int)x, -(int)y);

    // Show/hide depending on final alpha levelif (trans.getAlpha() > 0.5){
        view.setVisibility(VISIBLE);
    } else {
        view.setVisibility(INVISIBLE);       
    }
}

privatevoidapplyAnimation(final View view, final Animation anim){
    view.scrollTo(0, 0);
    view.setVisibility(VISIBLE);

    anim.setAnimationListener(newAnimationListener(){
        privatebooleandoEnd=true;

        @OverridepublicvoidonAnimationEnd(Animation animation) {
            if (doEnd){
                doEnd = false;
                setViewPos(view, animation, anim.getStartTime() + anim.getDuration());
            }
        }

        @OverridepublicvoidonAnimationRepeat(Animation animation) {
        }

        @OverridepublicvoidonAnimationStart(Animation animation) {
        }

    });

    view.startAnimation(anim);
}

Post a Comment for "Applying Successive Animations To Imageview In Android"