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 に答える