有没有一种方法可以同时动画多个视图?
我想要做的是转换动画:
我有5个TextView和4个彩色条(带有背景的简单RelativeLayout)。 在动画开始时,stips与TextViews堆叠在水平行中。 到最后,我希望所有TextView都堆叠在strip之间:
这是一个非常简单的绘图,但它展示了我想做的事情。 是否有使用动画实现此目的的方法,还是必须使用canvas动画?
您可以使用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();
您可以使用 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();
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);
}
请注意,同时进行的动画越多,速度就会越慢。
startAnimation
时,它会开始无效化自己,直到达到预期位置。当你同时在所有视图上调用它们时,它们将都调用invalidate()
,然后在下一次绘制过程中,每个视图将在它们的下一帧中被绘制。由于你无论如何都在UI线程上调用所有这些操作,直到从该方法返回之前,所有动画都不会启动。注意:在Honeycomb之前的动画框架中,只会移动视图的可视部分。用户将无法点击它。 - DeeV/**
* 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()方法的注释)。
minSdkVersion="14"
或更高版本,这样就可以访问平台动画API。” - RestInPeace