按下按钮时重复动画

时间:2016-12-06 11:37:52

标签: android xml android-studio animation

我有一个攻击动画,我想重复动画,只要攻击按钮点击所有我可以出现的只是一个动画节目,当按钮再次按下时动画将不再重复 这是我的动画代码

imgAttack = (ImageView) findViewById(R.id.imgAttack);
        imgAttack.setBackgroundResource(R.drawable.attack_anim);
        attackanimation=(AnimationDrawable)imgAttack.getBackground();

btnAtk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               press=+1;
                MaxHealth-=100;

                swordAtk.start();

               attackanimation.start();




                health.setProgress(MaxHealth);
                if(MaxHealth==0) {


                    health.setProgress(0);
                    Gold=Gold+1;
                    txtGold.setText("Gold:"+Gold);
                    SharedPreferences prefs = RpgActivity.this.getSharedPreferences(getString( R.string.PREF_FILE),MODE_PRIVATE);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putInt(getString(R.string.SDR),Gold);
                    editor.apply();
                }
            }
        });
  

这是我的xml for attack_anim.xml

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item android:drawable="@drawable/attak0001" android:duration="100" />
    <item android:drawable="@drawable/attak0002" android:duration="100" />
    <item android:drawable="@drawable/attak0003" android:duration="100" />
    <item android:drawable="@drawable/attak0004" android:duration="100" />
    <item android:drawable="@drawable/attak0005" android:duration="100" />
    <item android:drawable="@drawable/attak0006" android:duration="100" />
    <item android:drawable="@drawable/attak0007" android:duration="100" />

</animation-list>

1 个答案:

答案 0 :(得分:0)

您可以在attackAnimation.start()

之前执行此类操作
attackAnimation.stop();
attackAnimation.selectDrawable(0);

之后写attackAnimation.start()