首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android - RotateAnimation onClick按钮重启自身

Android - RotateAnimation onClick按钮重启自身
EN

Stack Overflow用户
提问于 2012-10-06 18:28:56
回答 2查看 911关注 0票数 0

这是当点击按钮时旋转图像的java代码。图像旋转它是完美的,但如果我再次点击按钮,当旋转尚未结束时,动画重新启动本身,而不是结束动画。我如何等待动画的结束?我发现了Animation.AnimationListener,我认为onAnimationEnd对我来说很好,但我无法将它集成到我的代码中……请帮帮我:-)

代码语言:javascript
复制
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);
    }

}
EN

回答 2

Stack Overflow用户

发布于 2012-10-06 18:34:57

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

代码语言:javascript
复制
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;

        }

     }
票数 1
EN

Stack Overflow用户

发布于 2012-10-06 18:40:59

从未尝试过,但可能会起作用

代码语言:javascript
复制
    buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            if(!floatingImage.hasTransientState())
               floatingImage.startAnimation(animationRotateCenter);
        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12758960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档