hermann
hermann

Reputation: 6295

Looping Android Animation - Three buttons showing up and disappearing one after another at the same spot

I have the following fragment that gets loaded when my app is first launched. It simply shows a button that looks like a banner and prompts the user to take a specific action.

Now, this button fades in and out in a 3 second period.

The button is color purple, but what I'd like to do, is make it fade in and out 3 times, purple first, then blue, then green. ( I have two more buttons/banners that I wanted to use. )

Purple button : 3sec fadein/fadeout + 6sec wait
Blue button : 3sec wait + 3sec fadein/fadeout + 3sec wait
Green button : 6sec wait + 3 sec fadein/fadeout

All that in an endless circle. How could I do that?

public class StartFragment extends Fragment
{
    AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f );

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.start, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        Button scanBanner = (Button) getView().findViewById(R.id.ScanBanner);

        fadeIn.setDuration(1500);

        scanBanner.startAnimation(fadeIn);

        fadeIn.setRepeatCount(Animation.INFINITE);
        fadeIn.setRepeatMode(Animation.REVERSE);
    }
}

Upvotes: 0

Views: 347

Answers (1)

Darrell
Darrell

Reputation: 2045

You could add an animation listener to change the color. Take care to only change color every second repeat to account for fading back out to zero first.

fadeIn.setAnimationListener(new AnimationListener() {
    private int cycle = 0;
    public void onAnimationRepeat() {
        cycle = (cycle + 1) % 6;
        if (cycle == 0) {
            scanBanner.setBackgroundResource(R.drawable.purple);
        } else if (cycle == 2) {
            scanBanner.setBackgroundResource(R.drawable.blue);
        } else if (cycle == 4) {
            scanBanner.setBackgroundResource(R.drawable.green);
        }
    }
});

Upvotes: 1

Related Questions