动画布局的兄弟姐妹在动画制作完成之前不动画

时间:2013-07-24 22:39:31

标签: android android-layout android-animation android-view

我正在使用Animator(http://developer.android.com/reference/android/animation/Animator.html)来添加和删除ViewGroup中的视图。当我删除它们时,视图会动画(它是“向上滑动”动作),但其他孩子不会做出反应(向上滑动以占用空间)直到完成动画。我假设是因为我使用的是动画视图直到动画之后才设置动画视图。在动画视图滑出时,有没有办法让其他视图向上滑动?这是相关代码

AnimatorListener mCloseListener = new AnimatorListener() {
        @Override public void onAnimationEnd(Animator arg0) {
            viewToAnimate.setVisibility(View.GONE);
        }
        @Override public void onAnimationStart(Animator arg0) { }
        @Override public void onAnimationCancel(Animator arg0) { }
        @Override public void onAnimationRepeat(Animator arg0) { }
    };

viewToAnimate.animate().translationYBy(-viewToAnimate.getMeasuredHeight())
            .setDuration(ANIMATION_DURATION).setInterpolator(new DecelerateInterpolator()).setListener(
                    mCloseListener);

1 个答案:

答案 0 :(得分:1)

首先,您没有使用Animator,而是ViewPropertyAnimator,这稍微不同,您正在尝试为视图翻译制作动画。看一下translation定义,很明显它被应用 post-layout ,所以我假设其他视图/视图gorup不参与并且不关心该属性值。< / p>

我相信,您需要设置margin或视图的其他位置的动画,以使ViewGroup了解配置更改。我认为可以用Property Animation来实现,但需要更多的编码。

这是一些微不足道的例子。通过向上/向下滑动添加/删除按钮应该从以下布局中激活:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentTop="true"
        android:id="@+id/btn1"
        android:layout_weight="1"
        android:text="Button 1"
        android:onClick="buttonClickListener" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_below="@id/btn1"
        android:id="@+id/btn2"
        android:layout_weight="1"
        android:text="Button 2: Remove button 1"
        android:onClick="buttonClickListener" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_below="@id/btn2"
        android:id="@+id/btn3"
        android:layout_weight="1"
        android:text="Button 3: Remove button 2"
        android:onClick="buttonClickListener" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_below="@id/btn3"
        android:id="@+id/btn4"
        android:layout_weight="1"
        android:text="Button 4: add removed button"
        android:onClick="buttonClickListener" />
</RelativeLayout>

然后活动可能如下所示:

public class MyActivity extends Activity implements Animator.AnimatorListener {

    private static final String TAG = "MyActivity";

    /** Queue to manage removed buttons */
    private LinkedBlockingDeque<Integer> mRemovedButtonDeque = new LinkedBlockingDeque<Integer>();
    /** Distance view should be moved up and down, from resources */
    private int mMovingDistance = 0;
    /** Animators to be used */
    private ObjectAnimator mMarginAnimator;
    /** Duration of all animations */
    private static final int ANIMATION_DURATION = 1000;
    /** Id of currently adding view */
    private int mAddingId = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Show the layout with the test view
        setContentView(R.layout.main);

        mMovingDistance = getResources().getDimensionPixelSize(R.dimen.button_height);

        mMarginAnimator = new ObjectAnimator().setDuration(ANIMATION_DURATION);
        mMarginAnimator.setInterpolator(new DecelerateInterpolator());

        mMarginAnimator.addListener(this);
    }

    @Override
    public void onAnimationStart(final Animator animation) {
        // nothing to do here
    }

    @Override
    public void onAnimationEnd(final Animator animation) {
        if (mAddingId == 0) {
            final Integer removingViewId = mRemovedButtonDeque.peek();

            if (removingViewId != null) {
                findViewById(removingViewId).setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onAnimationCancel(final Animator animation) {
        // nothing to do here
    }

    @Override
    public void onAnimationRepeat(final Animator animation) {
        // nothing to do here
    }

    /**
     * Listener for buttons click
     *
     * @param clickedButton {@link View}
     */
    public void buttonClickListener(final View clickedButton) {
        final int buttonId = clickedButton.getId();
        MarginAnimatorHelper animator = null;

        switch (buttonId) {
            case R.id.btn1:
                // nothing to do here
                break;

            case R.id.btn2:
                animator = new MarginAnimatorHelper((Button) findViewById(R.id.btn1));
                mRemovedButtonDeque.add(R.id.btn1);
                mMarginAnimator.setIntValues(0, -mMovingDistance);
                break;

            case R.id.btn3:
                if (findViewById(R.id.btn1).getVisibility() != View.VISIBLE) {
                    animator = new MarginAnimatorHelper((Button) findViewById(R.id.btn2));
                    mRemovedButtonDeque.add(R.id.btn2);
                    mMarginAnimator.setIntValues(0, -mMovingDistance);
                }
                break;

            case R.id.btn4:
                final Integer removedId = mRemovedButtonDeque.pollLast();

                if (removedId != null) {
                    final Button removedButton = (Button) findViewById(removedId);

                    animator = new MarginAnimatorHelper(removedButton);
                    removedButton.setVisibility(View.VISIBLE);
                    mMarginAnimator.setIntValues(-mMovingDistance, 0);
                }
                break;
        }

        if (animator != null) {
            mMarginAnimator.setTarget(animator);
            mMarginAnimator.setPropertyName(MarginAnimatorHelper.PROPERTY_NAME);
            mMarginAnimator.start();
        }
    }

    /**
     * This view will manages view removing
     * and act as {@link ObjectAnimator} target
     */
    private static class MarginAnimatorHelper {
        /** Name of the property to be animated */
        private static final String PROPERTY_NAME = "topMargin";

        /** Button to be animated */
        private final Button mButton;

        /**
         * Default constructor
         *
         * @param button {@link Button} to be animated
         */
        public MarginAnimatorHelper(final Button button) {
            mButton = button;
        }

        /**
         * Setter for property 'topMargin'
         *
         * @param margin new value of the property
         */
        public void setTopMargin(final int margin) {
            final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mButton.getLayoutParams();

            params.topMargin = margin;
            mButton.setLayoutParams(params);
        }
    }
}

内部类MarginAnimatorHelper完成所有主要工作:更改影响其他布局元素的实际视图布局参数。