Trần Hòa
Trần Hòa

Reputation: 235

Set animation rotation repeat back for layout

I want creat aniamtion rotation before it rotaion from 0 to -30 and after from -30 to 0 so I created two file animation xoay.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="-30"
    android:pivotX="50%"
    android:duration="6000"
    android:repeatCount="infinite"
    android:fillAfter="true" />

xoay2.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="-30"
    android:toDegrees="0"
    android:pivotX="50%"
    android:duration="6000"
    android:repeatCount="infinite"
    android:fillAfter="true" />

and in activity I start animation with code

final RotateAnimation rotate= (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.xoay);
final RotateAnimation rotate2= (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.xoay_2);

and when I set

myView.startAnimation(rotate); or myView.startAnimation(rotate2);
it run. Now I want when myView finish animation xoay.xml it will run xoay2.xml and when xoay2.xml finish it run xoay.xml... I tried with code :

AnimationSet s = new AnimationSet(false);
s.addAnimation(rotate);
s.addAnimation(rotate2);
myView.startAnimation(s)

but it not run. How I can do it? Thank you very much !

Upvotes: 2

Views: 2939

Answers (4)

Hossein Hajizadeh
Hossein Hajizadeh

Reputation: 1485

//ROTATION

ObjectAnimator.ofFloat(yourView, View.ROTATION, 0f, 180f).setDuration(300).start();

//ROTATION Back

ObjectAnimator.ofFloat(yourView, View.ROTATION, 180f, 0F ).setDuration(300).start();

Upvotes: 1

ND1010_
ND1010_

Reputation: 3841

I will create for you Just copy it and apply on your project

Step-1: Create Animation xml files

rotate_1.xml

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="1200" />

rotate_2.xml

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="1200" />

activity_main.xml

 <TextView
    android:id="@+id/tvDemo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:background="@color/colorAccent"
    android:layout_gravity="center"
    />

MainActivity.java

TextView tvDemo;
boolean isFirstTime;
RotateAnimation rotate;
RotateAnimation rotate2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvDemo = (TextView) findViewById(R.id.tvDemo);
    rotate = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotate_up);
    rotate2 = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotate_down);

    tvDemo.startAnimation(rotate);

    rotate.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            tvDemo.startAnimation(rotate2);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    rotate2.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            tvDemo.startAnimation(rotate);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });


    isFirstTime = false;
    tvDemo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isFirstTime = true;

        }
    });
}

I Hope this code will work for you

Thank you

Upvotes: 2

Paresh
Paresh

Reputation: 6857

Better you user Animators instead

AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
AnimatorSet animatorSet = new AnimatorSet();

ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(((StoryHolder) holder).ivLike, "rotation", 0f, -30f);
rotationAnim.setRepeatCount(ValueAnimator.INFINITE);
rotationAnim.setDuration(300);
rotationAnim.setInterpolator(ACCELERATE_INTERPOLATOR);

ObjectAnimator rotationAnim2 = ObjectAnimator.ofFloat(((StoryHolder) holder).ivLike, "rotation", 0f, -30f);
rotationAnim2.setRepeatCount(ValueAnimator.INFINITE);
rotationAnim2.setDuration(300);
rotationAnim2.setInterpolator(ACCELERATE_INTERPOLATOR);

animatorSet.play(rotationAnim).before(rotationAnim2);
animatorSet.start();

Upvotes: 1

SpiralDev
SpiralDev

Reputation: 7321

You can achieve that with RotateAnimation

Animation an = new RotateAnimation(0.0f, 30.0f);

// Set the animation's parameters
an.setDuration(6000);               // duration in ms
an.setRepeatCount(-1);                // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true);               // keep rotation after animation

// Aply animation to image view
myView.setAnimation(an);

Upvotes: 4

Related Questions