如何在android中同时启动两个动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10606053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start two animations at same time in android?
提问by Noby
I have two linear layouts which I want to perform two different animation on both of these layouts and at the same time.
我有两个线性布局,我想同时在这两个布局上执行两种不同的动画。
Now its working in sequential manner. i.e, after completing one its starting another.
现在它以顺序方式工作。即,在完成一个之后开始另一个。
here is my code.
这是我的代码。
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.menu:
mainLayout.startAnimation(outtoLeft);
sideBar.startAnimation(inFromRight);
break;
}
}
outtoLeft.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mainLayout
.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 40));
}
});
回答by Gophermofur
I think you need to use an AnimationSet
.
我认为您需要使用AnimationSet
.
From the doc:
从文档:
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform.
代表一组应该一起播放的动画。每个单独动画的变换组合在一起形成一个变换。
Hereyou can see how an AnimationSet
is supposed to be.
在这里你可以看到 anAnimationSet
应该是怎样的。
回答by Guillermo Mansilla
In your Activity
在您的活动中
ImageView reusableImageView = (ImageView)findViewById(R.id.imageView1);
reusableImageView.setImageResource(R.drawable.flag);
reusableImageView.setVisibility(View.VISIBLE);
Animation an = AnimationUtils.loadAnimation(this, R.anim.yourAnimation);
reusableImageView.startAnimation(an);
Then, in yourAnimation.xml define all the animations you want
然后,在 yourAnimation.xml 中定义您想要的所有动画
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="2500" />
<scale
android:startOffset="2500"
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.5"
android:toYScale="0.5" />
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
回答by nickeyzzz
I solved it starting second animation in onAnimationStart event of the first animation. In my example right layout replacing left one, both of them are moving left at the same time. I use animate property of a View class http://developer.android.com/reference/android/view/View.html#animate()which is available starting API v.12
我解决了它在第一个动画的 onAnimationStart 事件中启动第二个动画的问题。在我的示例中,右侧布局替换左侧布局,它们同时向左移动。我使用 View 类http://developer.android.com/reference/android/view/View.html#animate() 的animate 属性,该属性从 API v.12 开始可用
leftLayout.animate()
.translationX(-leftLayout.getWidth()) // minus width
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
rightLayout.animate()
.translationX(-leftLayout.getWidth()) // minus width
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
leftLayout.setVisibility(View.GONE);
leftLayout.setTranslationX(0f); // discarding changes
rightLayout.setTranslationX(0f);
}
});
}
});
回答by Tushar
The way to solve this problem is to use an AnimatorSetwith Animatorobjects. If you are worried about backwards compatibility, you can use the NineOldAndroids library to bring the API back all the way to Android 1.0
解决这个问题的方法是使用AnimatorSet和Animator对象。如果您担心向后兼容性,您可以使用 NineOldAndroids 库将 API 一路带回 Android 1.0