Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ImageView Rotate Animation working only once

Tags:

android

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" />

like image 299
Sayantan Mandal Avatar asked Oct 20 '25 13:10

Sayantan Mandal


2 Answers

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.

like image 78
Bmuig Avatar answered Oct 23 '25 04:10

Bmuig


Use rotationBy() instead of rotation();

like image 39
Jiachen Avatar answered Oct 23 '25 02:10

Jiachen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!