Multiple animations on 1 imageview android

后端 未结 5 1794
感动是毒
感动是毒 2021-02-02 10:42

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

相关标签:
5条回答
  • 2021-02-02 11:11

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

    0 讨论(0)
  • 2021-02-02 11:11

    Can be done programatically using AnimatorSet class of android :

    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 .....

    For complete example check this out..

    0 讨论(0)
  • 2021-02-02 11:11

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

    this will help you but first you should read the docs of 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>
    
    0 讨论(0)
  • 2021-02-02 11:20

    Use the AnimationSet class:

    AnimationSet s = new AnimationSet(false);//false means don't share interpolators
    s.addAnimation(traintween);
    s.addAnimation(trainfad);
    mytrain.startAnimation(s);
    
    0 讨论(0)
  • 2021-02-02 11:27

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

    0 讨论(0)
提交回复
热议问题