4

I am facing strange Issue If I click on the image it is rotating for first time, if you click it again it is not rotating. I have used toast to check if control is going inside the function, but toast is getting printed all the time.

Why Image is not rotating for second time? here is my code..

MainActivity:

package com.example.sayantan.myapp1;

import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ImageView imageView;

public void rotateImage(View view) {
    Toast.makeText(getApplicationContext(), "in rotateImage()...", Toast.LENGTH_SHORT).show();
    imageView.animate().rotation(1800f).setDuration(1500);
//        Toast.makeText(getApplicationContext(), "End of rotateImage()", Toast.LENGTH_SHORT).show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);

}

}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.sayantan.myapp1.MainActivity">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/imageView"
    android:layout_below="@+id/blue"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp"
    android:src="@drawable/bart"
    android:onClick="rotateImage" />

Sayantan Mandal
  • 1,246
  • 14
  • 20
  • Possible duplicate of [How to generate looping animation with ViewPropertyAnimator?](http://stackoverflow.com/questions/15815784/how-to-generate-looping-animation-with-viewpropertyanimator) – Selçuk Cihan Sep 23 '16 at 13:24

4 Answers4

5

I am pretty sure that the rotation is kept at the end of the animation. So the second time you try to animate the image it doesn't move because it already is rotated to 1800.

So either reset your image to 0 or rotate it to currentRotation + 1800f.

Bmuig
  • 1,059
  • 7
  • 12
5

Use rotationBy() instead of rotation();

Jiachen
  • 51
  • 1
  • 1
2

Have you tried to call Start()? imageView.animate().rotation(1800f).setDuration(1500).start();

Robin Bruneel
  • 1,063
  • 8
  • 22
1

hi you can try with this,

imageView.animate().rotation(1800f).setDuration(1500);
imageView.getAnimation().setRepeatCount(Animation.INFINITE);

Thanks hope this will help you.

Saveen
  • 4,120
  • 14
  • 38
  • 41