动画布局的同级直到动画完成后才设置动画



我正在使用Animator(http://developer.android.com/reference/android/animation/Animator.html)添加视图和从视图组中删除视图。当我移除它们时,视图会设置动画(这是"向上滑动"运动),但其他子对象在动画完成之前不会对动画做出反应(向上滑动以占据空间)。我之所以推测,是因为我使用的是在动画之后才将动画视图设置为GONE。当动画视图滑出时,有没有办法让其他视图向上滑动?这是相关的代码

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);

首先,您使用的不是Animator,而是ViewPropertyAnimator,这与您尝试设置视图转换的动画略有不同。看看翻译定义,很明显,它被应用于post layout,所以我认为其他视图/视图gorup不参与,也不关心属性值。

我认为,您需要为margin或视图的其他位置设置动画,以使ViewGroup了解配置更改。我认为它可以通过属性动画实现,但需要更多的编码。

下面是一些琐碎的例子。通过向上/向下滑动来说按钮添加/删除应该从以下布局中设置动画:

<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完成了所有主要工作:更改影响其他布局元素的真实视图布局参数。

最新更新