首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为View.setVisibility设置动画(GONE)

如何为View.setVisibility设置动画(GONE)
EN

Stack Overflow用户
提问于 2010-04-14 08:11:00
回答 4查看 65.3K关注 0票数 84

View的可见性设置为GONE时,我想创建一个AnimationView应该“崩溃”,而不是只是失望。我用ScaleAnimation试过了,但是View崩溃了,但是布局只会在Animation停止(或开始)之后(或之前)调整它的空间大小。

如何制作Animation,以便在动画时,较低的View%s将直接位于内容的正下方,而不是留出空白?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-17 19:06:29

似乎没有一种简单的方法可以通过API来做到这一点,因为动画只是改变视图的渲染矩阵,而不是实际的大小。但我们可以设置一个负边距来欺骗LinearLayout,使其认为视图正在变小。

因此,我建议您创建自己的基于ScaleAnimation的动画类,并覆盖"applyTransformation“方法来设置新的页边距和更新布局。像这样..。

代码语言:javascript
复制
public class Q2634073 extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.q2634073);
        findViewById(R.id.item1).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        view.startAnimation(new MyScaler(1.0f, 1.0f, 1.0f, 0.0f, 500, view, true));
    }

    public class MyScaler extends ScaleAnimation {

        private View mView;

        private LayoutParams mLayoutParams;

        private int mMarginBottomFromY, mMarginBottomToY;

        private boolean mVanishAfter = false;

        public MyScaler(float fromX, float toX, float fromY, float toY, int duration, View view,
                boolean vanishAfter) {
            super(fromX, toX, fromY, toY);
            setDuration(duration);
            mView = view;
            mVanishAfter = vanishAfter;
            mLayoutParams = (LayoutParams) view.getLayoutParams();
            int height = mView.getHeight();
            mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height;
            mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 1.0f) {
                int newMarginBottom = mMarginBottomFromY
                        + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime);
                mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,
                    mLayoutParams.rightMargin, newMarginBottom);
                mView.getParent().requestLayout();
            } else if (mVanishAfter) {
                mView.setVisibility(View.GONE);
            }
        }

    }

}

通常的警告是:因为我们覆盖了一个受保护的方法(applyTransformation),所以这不能保证在未来的安卓版本中也能工作。

票数 51
EN

Stack Overflow用户

发布于 2013-07-03 13:19:57

如果视图不在布局中,则将其放入布局中,并为该布局设置android:animateLayoutChanges="true"

票数 99
EN

Stack Overflow用户

发布于 2012-05-14 18:20:21

我使用了和Andy在这里介绍的相同的技术。为此,我编写了自己的Animation类,它使边距的值具有动画效果,从而使项的效果消失/出现。它看起来是这样的:

代码语言:javascript
复制
public class ExpandAnimation extends Animation {

// Initializations...

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    super.applyTransformation(interpolatedTime, t);

    if (interpolatedTime < 1.0f) {

        // Calculating the new bottom margin, and setting it
        mViewLayoutParams.bottomMargin = mMarginStart
                + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

        // Invalidating the layout, making us seeing the changes we made
        mAnimatedView.requestLayout();
    }
}
}

我在我的博客文章http://udinic.wordpress.com/2011/09/03/expanding-listview-items/上有一个完整的例子

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2634073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档