简体   繁体   English

Android Animation调整LinearLayout的高度

[英]Android Animation to adjust height of LinearLayout

I have 2 LinearLayouts with views within them held in a container LinearLayout that is using layout_weight to determine their sizes. 我有2个LinearLayouts,其中的视图保存在使用layout_weight确定其大小的容器LinearLayout中。 I am trying to shrink the top view when the user clicks inside the bottom view with an animation. 当用户单击带有动画的底视图时,我试图缩小顶视图。

I extended Animation with a class: 我通过一个类扩展了Animation:

public class ShrinkTopViewAnimation extends Animation {

    protected int mOriginalHeight;
    protected final LinearLayout mView;

    public ShrinkTopViewAnimation(LinearLayout view) {
        this.mView = view;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mOriginalHeight = height;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation transformation
    ) {
        int shrinkAmount = (int)(mOriginalHeight * interpolatedTime);
        ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
        layoutParams.height = mOriginalHeight - shrinkAmount;
        mView.setLayoutParams(layoutParams);
        mView.requestLayout();
    }

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

The BottomView onClick calls: BottomView onClick调用:

        ShrinkTopViewAnimation shrinkTopViewAnimation = new ShrinkTopViewAnimation(mTopLinearLayout);
        shrinkTopViewAnimation.setDuration(1000);
        mTopLinearLayout.startAnimation(shrinkTopViewAnimation);

I am very confused as to what is happening. 我对所发生的事情感到非常困惑。 What I am seeing is that the first time through the applyTransform the interpolatedTime is 0 so the .height is set to the exact same number that it was before. 我看到的是,第一次通过applyTransform的interpolatedTime为0,因此.height设置为与之前完全相同的数字。 But the next call to the applyTransformation a getHeight call to the mView gives a number way bigger than the starting height and at the end of the transform when interpolatedTime is 1 the view is back to the original size. 但是下一次对applyTransformation的调用以及对mView的getHeight调用给出的数字大于起始高度,并且在转换结束时,如果interpolatedTime为1,则视图将恢复为原始大小。 The visual effect is the top view jumps larger, then shrinks back to original size. 视觉效果是顶视图跳得更大,然后缩小到原始大小。

Both getHeight() and the initialize Height are listed as px in the documentation, and the LayoutParams.Height is listed as px. 文档中的getHeight()和初始化的Height均以px列出,而LayoutParams.Height则以px列出。 But it seems like there is some translation to dp possibly going on? 但是好像有一些dp翻译可能会继续吗?

Any ideas? 有任何想法吗?

To keep the animated result, use Animation.setFillAfter(true) . 要保留动画结果,请使用Animation.setFillAfter(true)

When set to true, the animation transformation is applied after the animation is over. 设置为true时,动画结束后将应用动画转换。

What you used here is View Animation . 您在此处使用的是“ View Animation This kind of animation only modify rendering transformation, so the actual size of view is not changed. 这种动画仅修改渲染变换,因此视图的实际大小不会更改。

If you need to changed the actual size, you have to set LayoutParams via a AnimationListener , or use a Property Animation . 如果需要更改实际大小,则必须通过AnimationListener设置LayoutParams ,或使用Property Animation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM