2016-12-13 27 views
1

因此,我们试图制作一个动画,在FAB消失并打开工具栏之前移动FAB。问题是当我运行TranslationAnimation时,FAB消失,然后从屏幕滑出。 fromxDelta和FromDelta从来没有像我期望的那样行事。翻译动画从屏幕开始而不是它所属的位置

任何人都可以指出我做错了什么或帮助我了解这个调用的工作原理吗?

private void circularReveal(View toolbar, View fab) { 
    final View view1 = toolbar; 

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

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

    // create the animator for this view (the start radius is zero) 
    final SupportAnimator anim = ViewAnimationUtils.createCircularReveal(toolbar, cx, cy, 0, finalRadius); 
    //todo create an animation to move the fab. 

    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0f, 
                   Animation.ABSOLUTE , -100f, 
                   Animation.ABSOLUTE , 0f, 
                   Animation.ABSOLUTE , -100f); 
    translateAnimation.setDuration(2000L); 
    translateAnimation.setFillAfter(true); 
    Animation.AnimationListener animationListener = new Animation.AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      view1.setVisibility(View.VISIBLE); 
      anim.start(); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }; 
    translateAnimation.setAnimationListener(animationListener); 
    fab.startAnimation(translateAnimation); 
} 

编辑我的代码片段清理了一些变量名。

+0

你怎么称呼这个方法? circularReveal(fab,toolbar); ?因为您在名为工具栏的视图上应用translateAnimation。 –

+0

该方法是从 onClickListener调用的,该onClickListener在片段onCreatView()上的Fab上设置。 –

回答

1

因此,我们正试图制作一个动画,在其移动FAB之前它会消失并打开一个工具栏。

您的代码读取其他方式。它看起来像是在你的View上应用了一个名为'toolbar'的TranslateAnimation,并且在你的FloatingActionButton上使用了一个圆形的Reveal。

反正我猜你正在努力实现这样的事情:

private void circularReveal(final View view, View toolbar) { 

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

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

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

    view.animate() 
      .translationX(-100) 
      .translationY(-100) 
      .setDuration(2000) 
      .setListener(new Animator.AnimatorListener() { 
       @Override 
       public void onAnimationStart(Animator animation) { 

       } 

       @Override 
       public void onAnimationEnd(Animator animation) { 
        view.setVisibility(View.INVISIBLE); // set this to INVISIBLE if you want your FAB to dissapear 
        anim.start(); 
       } 

       @Override 
       public void onAnimationCancel(Animator animation) { 

       } 

       @Override 
       public void onAnimationRepeat(Animator animation) { 

       } 
      }); 

} 

这使FAB稍微向左和向上的工具栏显示和FAB自败了。

以FAB作为第一个参数调用此方法,并以ToolBar作为第二个参数。

我遗漏了TranslateAnimation并使用ViewPropertyAnimator,因为TranslateAnimation并不真正移动视图,而只是屏幕上的像素,而ViewPropertyAnimator会更改视图的实际位置。 TranslateAnimation的问题在于,您仍然可以点击FAB所在的旧位置,它仍会触发FAB OnClickListener。

+0

谢谢我清理这些变量名称。我试用了ViewPropertyAnimator,就像在你的例子中那样,它使得晶圆厂从屏幕上消失,只有在两秒钟之后重新出现在新的点上。 –

+0

所以我必须在我的片段布局xml的两个xml父项中设置属性android:animateLayoutChanges =“true”。一旦我做到了,动画显示正确。谢谢你的帮助。 –

相关问题