设置高度视图以匹配父动画在android中的动画

时间:2017-04-07 18:27:10

标签: java android xml animation android-linearlayout

如果有点击,如何将视图的高度更改为match_parent?

public class ResizeAnimation extends Animation {
    final int startHeight;
    final int targetHeight;
    private final boolean isOpen;
    View view;

    public ResizeAnimation(View view, int height, boolean isOpen) {
        this.view = view;
        this.targetHeight = height;
        this.isOpen = isOpen;
        startHeight = view.getHeight();
    }

    @Override
     protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight;
        if (isOpen) {
            newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
        } else {
            newHeight =  (int) (startHeight + targetHeight * interpolatedTime);
        }
        view.getLayoutParams().height = newHeight;
        view.requestLayout();
    }

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

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}
ResizeAnimation resizeAnimation = new ResizeAnimation(view, MATCH_PARENT, false);
resizeAnimation.setDuration(500);
view.startAnimation(resizeAnimation);

1 个答案:

答案 0 :(得分:1)

由于您将View.MATCH_PARENT(值 -1 )作为目标高度传递,因此您的动画无法正常工作。引用文档:

  

int MATCH_PARENT [...]   常数值: -1 (0xffffffff)

你必须通过真正的目标高度。您可以在渲染完成后在父版面中完成测量未来目标高度(我建议您ViewTreeObserver.onGlobalLayout())。