membersound
membersound

Reputation: 86915

How to make a flashing image animation?

I have an ImageView with alpha=0 by default. So it acts as an invisible overlay on another image.

On click, I want to create an animation that shows the overlay image for 200ms, and then hides it again.

The following does work in general, but only a single time! Why?

final ImageView flash = (ImageView) view.findViewById(R.id.flash);

flash.animate()
        .alpha(255) //make visible
        .setDuration(200)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                flash.setImageAlpha(0); //hide again
            }
        });

Upvotes: 0

Views: 122

Answers (1)

lelloman
lelloman

Reputation: 14183

you're setting 2 different alpha values, the first one is View's alpha, but when you end the animation you set the ImageView class alpha to 0, so if you start the animation again, the View alpha is 1.0f but the image alpha will be 0 and you see nothing. change it to

flash.animate()
        .alpha(1.f)
        .setDuration(200)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                flash.setAlpha(0.f);
            }
        });

View (and therefore ImageView) has a method setAlpha(float), ImageView adds also another method setAlpha(int), which is deprecated because it's confusing as hell and now it's been renamed to setImageAlpha(int). the animation will call View's setAlpha(float)

Upvotes: 3

Related Questions