【问题标题】:Shrink/Grow layout w/ animation on DialogFragmentDialogFragment 上带有动画的缩小/增长布局
【发布时间】:2018-09-03 00:11:32
【问题描述】:

我有一个DialogFragment,我正在尝试制作动画,以便当onClick()'ed 时,下方会出现一个确认信息。 我曾尝试将setVisibility() 与动画师一起使用,但这不是我想要的。我希望布局滑入,动画不会出现在之后,或者相反之前消失。

我一直在玩一些来自 Github 的代码 https://github.com/ThePreviousOne/Example `

    handle.setOnClickListener( new View.OnClickListener() {
            float startHeight;

            @Override
            public void onClick(View v) {

            startHeight = slideDownView.getHeight();

            // Adjust the slide down height immediately with touch movements.
                if (down) {
                LayoutParams params = slideDownView.getLayoutParams();
                params.height = (int) (startHeight - 300);
                slideDownView.setLayoutParams(params);
                down = false;
            } else {
                LayoutParams params = slideDownView.getLayoutParams();
                params.height = (int) (startHeight + 300);
                slideDownView.setLayoutParams(params);
                down = true;
            }
        }
    });

这可行,但我不知道如何将新代码连接到动画师,以便我可以控制片段调整大小的速度

【问题讨论】:

标签: android android-layout android-fragments android-animation


【解决方案1】:

您可以定义一个自定义动画器,在动画的每一帧更新高度属性。例如:

int startHeight = slideDownView.getHeight();
// Note that you should not hardcode "300" as that will be different pixel values on
// different devices - get the value from a dimen resource or scale by the
// device density
int endHeight = startHeight - getDistanceToAnimate();

// Create a simple int animator that animates between the starting and ending height
ValueAnimator animator = ValueAnimator.ofInt(startHeight, endHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        // On each frame, update the view height
        int value = (Integer) valueAnimator.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = value;
        view.setLayoutParams(layoutParams);
    }

    @Override
    public void onAnimationEnd(Animator animation) {
       // Once the animation finishes, you might have to update the view's final
       // height and / or its `layout_height` attribute.
    }
});

animator.setDuration(getAnimationTime());
animator.start();

希望有帮助!

【讨论】:

  • 这段代码使动画非常不稳定,我尝试调整它并再次寻找其他方法,但没有运气。非常感谢任何进一步的帮助。
  • 我在整个应用程序中广泛使用它来制作“展开”和“折叠”动画,效果很好。也许您的应用程序/设置还有其他原因导致它“不稳定”。抱歉,没有更多信息很难说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多