Android animation, moving and rotating at same time

前端 未结 1 1203
情话喂你
情话喂你 2021-01-14 08:06

I am working on animating an image view and that moves to the right and rotates at the same time. I can get the item to move using

    animation = new Transl         


        
相关标签:
1条回答
  • 2021-01-14 08:45

    You need to use an AnimationSet.

    An example in XML I made which moves an image from left to right, up and down and rotates, all at the same time.

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Rotate -->
        <rotate android:fromDegrees="30" android:toDegrees="0"
            android:pivotX="50%" android:pivotY="50%" android:repeatCount="4"
            android:repeatMode="reverse" android:duration="2000"
            android:interpolator="@android:anim/linear_interpolator" />
        <!--  Left to right -->
        <translate android:fromXDelta="-50%p" android:toXDelta="150%p"
            android:fromYDelta="100%p" android:toYDelta="0" android:duration="10000"
            android:interpolator="@android:anim/linear_interpolator" />
        <!--  Up and down bob -->
        <translate android:fromXDelta="0" android:toXDelta="0"
            android:fromYDelta="30" android:toYDelta="-30" android:repeatMode="reverse"
            android:repeatCount="4" android:interpolator="@android:anim/linear_interpolator"
            android:duration="2000" />
    </set>
    

    Code example (not to replicate above):

    AnimationSet animationSet = new AnimationSet(true);
    
    TranslateAnimation a = new TranslateAnimation(
            Animation.ABSOLUTE,200, Animation.ABSOLUTE,200,
            Animation.ABSOLUTE,200, Animation.ABSOLUTE,200);
    a.setDuration(1000);    
    
    RotateAnimation r = new RotateAnimation(0f, -90f,200,200);
    r.setStartOffset(1000);
    r.setDuration(1000);
    
    animationSet.addAnimation(a);
    animationSet.addAnimation(r);
    

    (Taken from here)

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