【问题标题】:How to revert a View back to height of "wrap_content" after animating it to be 0dp?将视图设置为 0dp 后如何将视图恢复为“wrap_content”的高度?
【发布时间】:2014-04-29 20:28:54
【问题描述】:

我有一个想要设置动画的 TextView。 最初,TextView 设置为 visibility=GONE,高度为 wrap_content。当用户执行特定操作时,TextView 设置为可见。几秒钟后,我想将 TextView 的高度从它的 wrap_content 高度设置为 0dp。我的工作没有任何问题。当动画结束时,我的问题就来了。动画完成后,我将 TextView 设置回 visibility=GONE 并尝试将 TextView 的高度重置为 wrap_content 但高度似乎没有改变。

我尝试将 TextView 的布局参数设置回 wrap_content 的高度,然后使 TextView 无效,但这不起作用。

知道该怎么做吗?

这是我目前制作动画的方式:

Animation slideUp = new Animation() {
    {
        setDuration(1000);
        setAnimationListener(new Animation.AnimationListener() {                
            @Override
            public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTxtTimerSet.getLayoutParams();
                lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                mTxtTimerSet.setVisibility(View.INVISIBLE);
                mTxtTimerSet.setLayoutParams(lp);
                mTxtTimerSet.invalidate();
                mTxtTimerSet.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTxtTimerSet.getLayoutParams();
        lp.height = mTxtTimerSet.getHeight() - ((int) (mTxtTimerSet.getHeight() * interpolatedTime));
        mTxtTimerSet.setLayoutParams(lp);
    }
};

mTxtTimerSet.startAnimation(slideUp);

谢谢

【问题讨论】:

    标签: android animation layout


    【解决方案1】:

    动画完成后如何检查 TextBox 的高度?也许您想使用visibility=View.INVISIBLE 而不是View.GONE
    如果您想再次为其设置动画,您可以尝试在动画开始时指定宽度。

    【讨论】:

    • 我在最初的帖子中添加了一些示例代码,以准确展示我是如何制作动画的。我没有提到在无效之前将其设置为 INVISIBLE,然后将其设置为 GONE。
    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2020-04-14
    相关资源
    最近更新 更多