Search code examples
androidanimationandroid-5.0-lollipop

CircularReveal animation doesn't work on first attempt


In android 5.0 i am trying to work with circular reveal animation

Problem

When i click on button to start reveal animation, on first click animation doesn't start

Second Click onwards it works normally

My Code

public class MainActivity extends ActionBarActivity {

Animator a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final View cardType = findViewById(R.id.cardtype);
    cardType.setVisibility(View.GONE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        a = ViewAnimationUtils.createCircularReveal(cardType,
                cardType.getWidth(),
                cardType.getHeight(),
                0,
                cardType.getHeight() * 2)
                .setDuration(2500);
        a.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                cardType.setVisibility(View.VISIBLE);
            }
        });
        a.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                cardType.setVisibility(View.GONE);
            }
        });
        findViewById(R.id.icon_first_activity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a.start();
            }
        });
    }
}}

Solution

  • I haven't tried your code, but I think you have a small ordering problem. I think you just need to set the cardType visible before you start the animation.

    Edited to add:

    ... and you should be setting your button View.INVISIBLE, not View.GONE.

    Here: This code works.

    Edited once more to add:

    Yes. Your problem is that you set the view GONE initially. That means it has 0 size. Then you use cardType.getHeight and cardType.getWidth as reveal coordinates. They are 0. You are going to want to set the view INVISIBLE, initially, and then use width/2 and height/2 as the center of the reveal.