Android L reveal Effect is not working

本秂侑毒 提交于 2019-12-21 09:25:21

问题


I tried to implement createCircularReveal() in FloatingButton. But the animation is too fast. How to increase the duration of the Animation. I tried setDuration(milli-seconds), But its not working.

I follow developer.android.com, Defining Custom Animations

Here my code:

int cx = (fabBtn.getLeft() + fabBtn.getRight()) / 2;
int cy = (fabBtn.getTop() + fabBtn.getBottom()) / 2;

int finalRadius = Math.max(fabBtn.getWidth(), fabBtn.getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(fabBtn, cx, cy, 2, finalRadius);
anim.setDuration(2000);
fabBtn.setVisibility(View.VISIBLE);
anim.start();

回答1:


I am also facing the issue as I mentioned and I am able to resolve this issue by getting cx and cy the center position for image view (In your case I think it is button).
So get cx and cy using this:

int cx = (fabBtn.getWidth()) / 2;
int cy = (fabBtn.getHeight()) / 2;

Instead of this::

int cx = (fabBtn.getLeft() + fabBtn.getRight()) / 2;
int cy = (fabBtn.getTop() + fabBtn.getBottom()) / 2;



回答2:


You might be calling getWidth() and getHeight() too soon.

So you'll have to use a getViewTreeObserver()

Be sure to add a duration to anim.setDuration(time) and set the initial visibility of the view to INVISIBLE

Here the code:

public void checker() {
    myView.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    int finalHeight = myView.getMeasuredHeight();
                    int finalWidth = myView.getMeasuredWidth();
                    // Do your work here
                    myView.getViewTreeObserver().removeOnPreDrawListener(this);

                    cx = finalHeight / 2;
                    cy = finalWidth / 2;


                    finalRadius = Math.max(finalHeight, finalWidth);
                    anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
                    myView.setVisibility(View.VISIBLE);
                    anim.setDuration(3000);
                    anim.start();


                    return true;
                }
            });

}



回答3:


I thought I had the same problem, but it seems that, for some reason, a duration of just 2000 milliseconds is not enough for the animation to show. When I set the duration to 3000, I saw a beautiful circle animation.

A small delay of 1 second also helped

                // get the center for the clipping circle
                int cx = myView.getWidth() / 2;
                int cy = myView.getHeight() / 2;

                // get the final radius for the clipping circle
                int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

                // create the animator for this view (the start radius is zero)
                Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
                anim.setStartDelay(1000);

                // make the view visible when the animation starts
                anim.addListener(new AnimatorListenerAdapter()
                {
                    @Override
                    public void onAnimationStart(Animator animation)
                    {
                        super.onAnimationStart(animation);
                        myView.setVisibility(View.VISIBLE);
                    }
                });

                // start the animation
                anim.start();


来源:https://stackoverflow.com/questions/27518327/android-l-reveal-effect-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!