Search code examples
androidanimationviewcircularreveal

Android circular reveal animation too fast


I am using the circular reveal animation in my project, but it is now working correctly. The problem is the reveal happens way to fast and you almost can't see the reveal because it expands like instantly. I tried setting anim.setDuration() but that did not change anything. I used the code from the google examples.

Here my code: View myView = getActivity().findViewById(R.id.view_to_expand);

int cx = myView.getRight();
int cy = myView.getBottom();

int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);

myView.setVisibility(View.VISIBLE);
anim.start();

view_to_expand is a simple relativelayout, don't think the problem is there. Also how can I apply the circular reveal to animation transitions?


Solution

  • The instantaneous expansion is because there is some other heavy work on main thread aside from your animation (data update, other views/fragments hiding, render refresh and so on).

    Best bet is to wrap it into a runnable and add it to the stack. It will be called when there is available CPU cycle and will show your animation.

    Below is the right way to create and use your runnable. Try not to post an anonymous one since there is possibility user to return and your runnable to hang and expose a memory leak and/or to throw a runtime exception.

    I presume here your class where your view stays is an activity and your myView is an instance reference in your activity

    public final ApplicationActivity extends Activity {
        private View myView;
    
        private final Runnable revealAnimationRunnable = new Runnable() {
            @Override
            public void run() {
                int cx = myView.getRight();
                int cy = myView.getBottom();
    
                int finalRadius = Math.max(myView.getWidth(), myView.getHeight());
                Animator animator = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
                animator.start();
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            myView = findViewById(R.id.view_to_expand);
            myView.setVisibility(View.VISIBLE);
            myView.post(revealAnimationRunnable);
            // alternatively, in case load is way too big, you can post with delay
            // i.e. comment above line and uncomment the one below
            // myView.postDelayed(revealAnimationRunnable, 200);
        }
    
        @Override
        protected void onDestroy() {
            ...
            myView.removeCallbacks(revealAnimationRunnable);
        }
    }