我如何动画View.setVisibility(GONE)动画、View、GONE、setVisibility

由网友(習慣了孤單)分享简介:我想打一个动画为当查看得到它的可见性设置为 GONE 。而不是仅仅dissapearing,在查看应崩溃。我想这与 ScaleAnimation 但随后的查看倒塌,但布局将只调整它的后空间(或前)动画停止(或启动)。I want to make an Animation for when a View gets it...

我想打一个动画为当查看得到它的可见性设置为 GONE 。而不是仅仅dissapearing,在查看应崩溃。我想这与 ScaleAnimation 但随后的查看倒塌,但布局将只调整它的后空间(或前)动画停止(或启动)。

I want to make an Animation for when a View gets it's visibility set to GONE. Instead of just dissapearing, the View should 'collapse'. I tried this with a ScaleAnimation but then the View is collapse, but the layout will only resize it's space after (or before) the Animation stops (or starts).

我怎样才能让动画这样一来,虽然动画,下查看旨意直接保持低于而不是有一个空格内容?

How can I make the Animation so that, while animating, the lower Views will stay directly below the content, instead of having a blank space?

推荐答案

似乎没有成为一个简单的方法通过API来做到这一点,因为动画只是改变视图的渲染矩阵,而不是实际尺寸。但是,我们可以通过设置来欺骗的LinearLayout,以为该观点正变得越来越小阴性切缘。

There doesn't seem to be an easy way to do this through the API, because the animation just changes the rendering matrix of the view, not the actual size. But we can set a negative margin to fool LinearLayout into thinking that the view is getting smaller.

所以,我建议你创建自己的动画类,根据ScaleAnimation,并覆盖了applyTransformation方法来设置新的利润和更新的布局。像这样...

So I'd recommend creating your own Animation class, based on ScaleAnimation, and overriding the "applyTransformation" method to set new margins and update the layout. Like this...

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),并不保证在Android的未来版本的工作

The usual caveat applies: because we are overriding a protected method (applyTransformation), this is not guaranteed to work in future versions of Android.

阅读全文

相关推荐

最新文章