【问题标题】:set height view to match parent with animation in android设置高度视图以匹配父级与android中的动画
【发布时间】:2017-04-07 18:27:10
【问题描述】:

当有点击时如何将view的高度改为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);

【问题讨论】:

    标签: java android xml animation android-linearlayout


    【解决方案1】:

    您的动画不起作用,因为您将 View.MATCH_PARENT(其值为 -1)作为目标高度传递。引用文档:

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

    你必须通过真实的目标高度。您可以在父布局渲染后测量其未来的目标高度(为此我推荐您ViewTreeObserver.onGlobalLayout())。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2017-09-03
      • 1970-01-01
      • 2021-03-24
      相关资源
      最近更新 更多