【问题标题】:Api 21 Circular Reveal Animation not workingApi 21 Circular Reveal 动画不起作用
【发布时间】:2015-12-12 16:55:00
【问题描述】:

我无法让圆形显示动画工作。 我想我检查了最明显的事情: 它开始,宽度和高度都> 0并且它是可见的,没有异常..

我从 Internet 加载一些数据并将其显示在视图中(fab) 动画只能在下载完成后播放。

TmdbHelper helper = new TmdbHelper();
                helper.getMovieById(id, "en", new TmdbHelper.ResultListener() {
                    @Override
                    public void onResultReceived(JSONObject result) {

                        // called when finished downloading

                        try {
                            String rating = result.getString("vote_average");

                            AnimationHelper.circularReveal(fab, 500, 0); 

                            fab.setText(rating);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

动画助手:

    public static void circularReveal(final View view, final long duration, long startDelay) {

    // get the center for the clipping circle
    int cx = (view.getLeft() + view.getRight()) / 2;
    int cy = (view.getTop() + view.getBottom()) / 2;

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

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

    anim.setDuration(duration);
    anim.setStartDelay(startDelay);

    anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

            view.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {}
    });

    // make the view visible and start the animation

    anim.start();
}

我在其他部分使用圆形显示动画,以确保视图已附加,并且可以正常工作:

headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    AnimationHelper.circularReveal(headerContainer, 500, 200);
                }
            });

【问题讨论】:

    标签: android animation android-5.0-lollipop


    【解决方案1】:

    也许您应该删除 onResultRecieved() 中的这一行:

    fab.setVisibility(View.VISIBLE);
    

    我的假设是您的循环显示方法运行良好。这是因为你在动画开始之前就已经让 FAB 可见,所以你看不到它的动作。

    此外,您所显示的那些正在运行的行在其中的任何地方都没有调用 fab.setVisibility(View.VISIBLE)

    【讨论】:

    • 我只是后来添加的,没有它也不起作用...只是忘记再次删除它顺便说一句:它在xml中设置为“不可见”
    • 那么我没有想法了.. 但既然你提到代码在其他地方都可以工作,这不能是透露的错。您确定正在调用onResultRecieved() 吗?
    • 是的。 Button 出现了,带有我在 onResultReceived 中设置的数据,但没有动画...
    • 您忘记启动动画师了。此外,根据它的放置方式,您可能会遇到位置问题。动画器的中心是相对于视图的,所以你应该使用 getWidth()/2 和 getHeight()/2 作为中心。
    • @GeorgeMount 最后一句话应该有更好的文档记录。特别是因为developer.android.com/training/material/animations.html#Reveal 错误地使用了 int cx = (myView.getLeft() + myView.getRight()) / 2;而不是 int cx = myView.getWidth() / 2
    【解决方案2】:

    第一种方法: 试试转换监听器。

                    getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
                        @Override
                        public void onTransitionStart(Transition transition) {
    
                        }
    
                        @Override
                        public void onTransitionEnd(Transition transition) {
    
                        }
    
                        @Override
                        public void onTransitionCancel(Transition transition) {
    
                        }
    
                        @Override
                        public void onTransitionPause(Transition transition) {
    
                        }
    
                        @Override
                        public void onTransitionResume(Transition transition) {
    
                        }
                    });
    

    第二种方法:尝试设置显示动画的启动延迟和侦听器,并在动画开始时将视图设置为可见

        if (Build.VERSION.SDK_INT >= 21) {
            Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius);
            anim.setStartDelay(300);
            anim.setDuration(1000);
            anim.setInterpolator(new DecelerateInterpolator());
            anim.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
    
                    viewRoot.setVisibility(View.VISIBLE);
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
    
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
            anim.start();
        }
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });
            anim.start();
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多