多个动画在1 imageview android。

时间:2023-01-28 10:47:04

I have 2 animations which are already working, i want to fade my train + tween my train on the same time. If I execute 1 of these lines it works. But if I try to execute both it, only 1 will work.. I really can't find a solution here.

我有两个已经在工作的动画,我想在同一时间让我的列车逐渐消失。如果我执行其中的一行,它就会工作。但如果我同时执行这两项,只有一项有效。我实在找不到解决办法。

Maybe you can help?

也许你可以帮助吗?

  final ImageView mytrain = (ImageView) findViewById(R.id.train);
  final Animation traintween = AnimationUtils.loadAnimation(this,R.anim.treinanimation);
   final Animation trainfade = AnimationUtils.loadAnimation(this,R.anim.trainfade);


  mytrain.startAnimation(trainfade);
 mytrain.startAnimation(trainntween);

I want mytrain to execute both animations..

我想让mytrain执行这两个动画。

Thank you for the help!

谢谢你的帮助!

5 个解决方案

#1


57  

Use the AnimationSet class:

使用AnimationSet类:

AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(traintween);
s.addAnimation(trainfad);
mytrain.startAnimation(s);

#2


3  

You need to use an AnimationSet, check out the docs. Just call addAnimation() for each Animation you want to play.

您需要使用一个AnimationSet,查看文档。只需为要播放的每个动画调用addAnimation()。

#3


3  

Can be done programatically using AnimatorSet class of android :

可以使用android的AnimatorSet类进行编程:

final AnimatorSet mAnimatorSet = new AnimatorSet();
    mAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0)
        );

//define any animation you want,like above

mAnimatorSet.setDuration(2000); //set duration for animations
    mAnimatorSet.start();

this example will start all the 3 animation on the target view(imgView) at same time ,you can also use playSequentially .....

此示例将同时启动目标视图(imgView)上的所有3个动画,您还可以按顺序使用……

For complete example check this out..

为完整的例子检查这个。

#4


1  

here is the example of all animation in a single xml file...

下面是一个xml文件中所有动画的示例……

this will help you but first you should read the docs of AnimationSet

这将对您有所帮助,但首先您应该阅读AnimationSet的文档

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:fromXScale="1.0" android:toXScale="3.0"
    android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%"
    android:pivotY="50%"  android:duration="5000" />
<set>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.2" android:toAlpha="1.0"     android:duration="3000" />
    <rotate android:fromDegrees="0" android:toDegrees="-360"
        android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"
        android:startOffset="700" android:duration="4000" />
<!--        <translate android:fromXDelta="0%" android:toXDelta="0%" -->
<!--            android:fromYDelta="0%" android:toYDelta="100%"     android:duration="3000" -->
</set>

</set>

#5


0  

you also can use the ImageSwitcher, i think this is better then AnimationSet in your case

你也可以使用ImageSwitcher,我认为这个比AnimationSet更好

#1


57  

Use the AnimationSet class:

使用AnimationSet类:

AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(traintween);
s.addAnimation(trainfad);
mytrain.startAnimation(s);

#2


3  

You need to use an AnimationSet, check out the docs. Just call addAnimation() for each Animation you want to play.

您需要使用一个AnimationSet,查看文档。只需为要播放的每个动画调用addAnimation()。

#3


3  

Can be done programatically using AnimatorSet class of android :

可以使用android的AnimatorSet类进行编程:

final AnimatorSet mAnimatorSet = new AnimatorSet();
    mAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(img_view,"scaleX",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"scaleY",1,0.9f,0.9f,1.1f,1.1f,1),
                ObjectAnimator.ofFloat(img_view,"rotation",0 ,-3 , -3, 3, -3, 3, -3,3,-3,3,-3,0)
        );

//define any animation you want,like above

mAnimatorSet.setDuration(2000); //set duration for animations
    mAnimatorSet.start();

this example will start all the 3 animation on the target view(imgView) at same time ,you can also use playSequentially .....

此示例将同时启动目标视图(imgView)上的所有3个动画,您还可以按顺序使用……

For complete example check this out..

为完整的例子检查这个。

#4


1  

here is the example of all animation in a single xml file...

下面是一个xml文件中所有动画的示例……

this will help you but first you should read the docs of AnimationSet

这将对您有所帮助,但首先您应该阅读AnimationSet的文档

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:fromXScale="1.0" android:toXScale="3.0"
    android:fromYScale="1.0" android:toYScale="3.0" android:pivotX="50%"
    android:pivotY="50%"  android:duration="5000" />
<set>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.2" android:toAlpha="1.0"     android:duration="3000" />
    <rotate android:fromDegrees="0" android:toDegrees="-360"
        android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"
        android:startOffset="700" android:duration="4000" />
<!--        <translate android:fromXDelta="0%" android:toXDelta="0%" -->
<!--            android:fromYDelta="0%" android:toYDelta="100%"     android:duration="3000" -->
</set>

</set>

#5


0  

you also can use the ImageSwitcher, i think this is better then AnimationSet in your case

你也可以使用ImageSwitcher,我认为这个比AnimationSet更好