0

这是单击按钮时旋转图像的java代码。图像旋转它是完美的,但如果我在旋转未结束时再次单击按钮,动画会重新启动,而不是结束动画。如何等待动画结束?我找到了 Animation.AnimationListener,我认为 onAnimationEnd 对我很有用,但我无法将它集成到我的代码中......请帮助我:-)

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);
    }

}
4

2 回答 2

1

您必须采用一个布尔变量来跟踪动画是否正在进行中。然后在动画侦听器中使用此变量并单击按钮,如下代码

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;

        }

     }
于 2012-10-06T10:34:56.527 回答
0

从来没有试过这个,但可能会奏效

    buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            if(!floatingImage.hasTransientState())
               floatingImage.startAnimation(animationRotateCenter);
        }
    });
于 2012-10-06T10:40:59.470 回答