【问题标题】:How to smoothen Linear layout height animation, having heavy content如何平滑线性布局高度动画,内容繁重
【发布时间】:2016-06-06 13:13:37
【问题描述】:
Animation animation = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            ViewGroup.LayoutParams params = slidingPane.getLayoutParams();
            int calculatedHeight = expandedHeight - ((int) (expandedHeight * interpolatedTime));
            if (calculatedHeight <= collapsedHeight) {
                calculatedHeight = collapsedHeight;
            }
            params.height = calculatedHeight;
            slidingPane.setLayoutParams(params);
            slidingPane.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }

    };
    animation.setDuration(ANIMATION_DURATION);
    animation.setAnimationListener(animationListener);
    slidingPane.startAnimation(animation);

SlidingPane 是一个 LinearLayout 并且它有一个 ListView 作为一个孩子。 ListView 包含每一行中的图像。
现在这段代码工作得很好,但动画不流畅。如果我从列表视图中删除图像,那么它工作正常。
我已经根据类似问题的 SO 答案尝试了以下操作 -

1. 在动画之前隐藏滑动窗格的内容,然后再次使其在动画结束时可见。它有帮助,但行为看起来很奇怪
2.在xml中设置android:animateLayoutChanges="true",但它没有任何动画



有没有办法解决这个问题?

【问题讨论】:

  • 尝试在动画中设置插值器,如:animation.setInterpolator(new LinearInterpolator());
  • @Mehta 能否请您发布整个代码。我对那一行没有太多想法:)
  • @Mehta 我使用了上面提到的代码,但它的性能比我的差。

标签: android android-layout android-animation android-linearlayout


【解决方案1】:

您应该尝试使用更系统的动画,而不是手动更改每个动画步骤的高度,例如ValueAnimator,或ObjectAnimator

slidingPane.animate().scaleY(0f).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(ANIMATION_DURATION);

可以在视图上使用pivotY/pivotY来控制缩放动画的锚点。

相关文档: https://developer.android.com/reference/android/animation/ValueAnimator.html https://developer.android.com/reference/android/animation/ObjectAnimator.html https://developer.android.com/guide/topics/graphics/prop-animation.html

【讨论】:

  • 这将缩放布局,不会改变高度,它会导致滑块的可见部分拉伸
  • 您希望它如何制作动画而不是拉伸?在包含的列表中显示更多/更少的项目,而不改变它们的大小?
  • 它应该像 NavigationDrawer 一样工作,只是从下到上
  • 如果我没记错的话,NavigationDrawer 动画不会改变它的高度,但是它会滑动它(translationY),如果没问题,试试:slidingPane.animate().translateY(0f)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2011-06-30
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多