0

我正在使用 Animator ( http://developer.android.com/reference/android/animation/Animator.html ) 从 ViewGroup 添加和删除视图。当我删除它们时,视图动画(它是“向上滑动”运动)但其他孩子在动画完成之前不会对动画做出反应(向上滑动以占用空间)。我假设是因为我在动画之后才使用未将动画视图设置为 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);
4

1 回答 1

1

首先,您没有使用Animator,而是ViewPropertyAnimator,它略有不同,您正在尝试为视图翻译设置动画。查看翻译定义,很明显它在布局后得到应用,所以我假设其他视图/视图组不参与并且不关心该属性值。

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

于 2013-07-26T09:12:16.390 回答