user52252
user52252

Reputation: 43

Android - RotateAnimation onClick button restart itself

this is the java code to rotate an image when click on button. Image rotate it's perfect but if I click button again when rotation is not ended the animation restart itself, not end the animation. How can I wait the end of the animation? I found Animation.AnimationListener, I think that onAnimationEnd works great for me, but I'm not be able to integrate it in my code... please help me :-)

package com.example.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class Rotation_test extends Activity {

    private float statdegree = (float) 0.0;
    private float enddegree = (float) 90.0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotation_test);

        // ---------------------------------------------------------------------------------------------
        Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
        final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);

        // AnimationRotation
        final Animation animationRotateCenter = new RotateAnimation(statdegree,
                enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animationRotateCenter.setDuration(5000L);
        animationRotateCenter
                .setInterpolator(new AccelerateDecelerateInterpolator());
        // \AnimationRotation

        buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View arg0) {
                floatingImage.startAnimation(animationRotateCenter);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Upvotes: 0

Views: 930

Answers (2)

nandeesh
nandeesh

Reputation: 24820

Never tried this, but might work

    buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            if(!floatingImage.hasTransientState())
               floatingImage.startAnimation(animationRotateCenter);
        }
    });

Upvotes: 0

Maneesh
Maneesh

Reputation: 6128

You have to take a boolean variable which tracks whether animation is in progress or not. then use this variable in animation listener and button click as below code

buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View arg0) {
if(!anim_in_progress)
                floatingImage.startAnimation(animationRotateCenter);
            }
        });
    animationRotateCenter.setAnimationListener(new anim_listener());

}
boolean anim_in_progress=false;

class anim_listener implements AnimationListener
     {

        @Override
        public void onAnimationEnd(Animation arg0) {
        anim_in_progress=false;


        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub
            anim_in_progress=true;

        }

     }

Upvotes: 1

Related Questions