单个动画 - 多个视图

时间:2012-02-09 19:21:10

标签: android animation views

有没有办法同时为多个视图制作动画?

我想要做的是翻译动画:

我有5个TextView和4个彩色条带(带有背景的普通RelativeLayouts)。在动画开始时,stips与TextViews在水平行中堆叠。最后,我想要在条带之间堆叠所有TextView:

enter image description here

这是一张非常简单的图画,但它展示了我想要做的事情。有没有办法用动画做这个,或者我必须使用画布动画。

4 个答案:

答案 0 :(得分:52)

您可以使用ObjectAnimator为多视图设置动画,如下所示:

ArrayList<ObjectAnimator> arrayListObjectAnimators = new ArrayList<ObjectAnimator>(); //ArrayList of ObjectAnimators

ObjectAnimator animY = ObjectAnimator.ofFloat(view, "y", 100f);
arrayListObjectAnimators.add(animY);

ObjectAnimator animX = ObjectAnimator.ofFloat(view, "x", 0f);
arrayListObjectAnimators.add(animX);
...
ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(objectAnimators);
animSetXY.setDuration(1000);//1sec
animSetXY.start();

答案 1 :(得分:4)

创建动画对象,然后在所有视图上同时使用startAnimation。所以它会是这样的:

TranslateAnimation anim1;
TranslateAnimation anim2;
TranslateAnimation anim3;

// Setup the animation objects

public void startAnimations()
{
   //... collect view objects
   view1.startAnimation(anim1);
   view2.startAnimation(anim2);
   view3.startAnimation(anim3);
}

请注意,您一次拍摄的动画越多,它就越慢。

答案 2 :(得分:3)

我设法在多个视图中共享一个动画实例。至少使用AlphaAnimation。我有一个ListView和一个应该应用于所有列表项视图的特定子项的动画。在我的情况下,视图应该可以随时“加入”和“离开”共享动画,它不应以任何方式影响其他动画视图或干扰已经运行的动画。 为了达到这个目的,我不得不对android的股票AlphaAnimation进行调整。 我的用例相当特殊,但是为了防止有人不得不用ListView来处理类似的目标,请将它放在这里。

/**
 * This class is a copy of android's stock AlphaAnimation with two adjustments:
 * - fromAlpha, toAlpha and duration are settable at any time.
 * - reset() method can be blocked. Reason: view.setAnimation() calls animation's reset() method
 * which is not intended in our use case.
 * For every new list item view we call setAnimation once for it's life time
 * and animation should not be reset because it is shared by all list item views and may be in progress. 
 */
public class MutableAlphaAnimation extends Animation {
    private float mFromAlpha;
    private float mToAlpha;
    private boolean resetBlocked;

    public MutableAlphaAnimation() {
    }

    public void start(float fromAlpha, float toAlpha, long duration) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
        setDuration(duration);
        setStartTime(START_ON_FIRST_FRAME);
    }

    public void setResetBlocked(boolean resetBlocked) {
        this.resetBlocked = resetBlocked;
    }

    @Override
    public void reset() {
        if (! resetBlocked) super.reset();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float alpha = mFromAlpha;
        t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));
    }

    @Override
    public boolean willChangeTransformationMatrix() {
        return false;
    }

    @Override
    public boolean willChangeBounds() {
        return false;
    }
}

将此动画设置为视图:

            animation.setResetBlocked(true);
            view.setAnimation(animation);
            animation.setResetBlocked(false);

要开始动画(之前由setAnimation设置),必须完成两件事:

        animation.start(0.0f, 1.0f, FADE_IN_DURATION);

之后,您必须在受动画影响的每个视图上手动调用invalidate()。

通常的startAnimation()会为你做invalidate(),但setAnimation没有。 (阅读android的源代码中关于View.setAnimation()方法的评论)。

答案 3 :(得分:3)

您可以使用AnimationSet

AnimatorSet decSet2 = new AnimatorSet();
        decSet2.playTogether(
                ObjectAnimator.ofFloat(view, "x",dX),
                ObjectAnimator.ofFloat(view, "y",dY),
                ObjectAnimator.ofFloat(mTextCancel, "x",dX),
                ObjectAnimator.ofFloat(mTextCancel, "y", dY),
                ObjectAnimator.ofArgb(mBtnOne, "visibility", View.VISIBLE, View.GONE),
                );
        decSet2.setDuration(0);
        decSet2.start();
相关问题