一个接一个地运行按钮的动画

时间:2015-03-11 08:25:28

标签: android animation

我使用动画显示我的按钮,我有9个按钮所以我使用持续时间进行显示,但它同时运行所有按钮的动画,只是持续时间不同,当然这是正确的,但我想运行第一个动画为button1,当它完成时,再次为button2动画,当它完成时,为下一个按钮执行.... 我的意思是一个接一个地为不同的按钮以相同的速度运行相同的动画,而不是同一时间现在我的apk是这样的: https://drive.google.com/file/d/0B9Q0pN8FVwEORGRQVE1kQmtvS28/view?usp=sharing

必须是这样的:

http://up.vbiran.ir/uploads/4517142605716735676_animationn.gif

我的完整代码:

    public class MainActivity extends ActionBarActivity {
    public MediaPlayer player;
    public MediaPlayer playerp;

    private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
            new ButtonSupport(R.id.Button06, 1000l, YourClassActivity.class),
            new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
            new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
            new ButtonSupport(R.id.Button08, 5000l,YourClassActivity4.class),
            new ButtonSupport(R.id.Button04, 6000l,YourClassActivity5.class),
            new ButtonSupport(R.id.Button01, 7000l,YourClassActivity6.class),
            new ButtonSupport(R.id.Button07, 9000l,YourClassActivity7.class),
            new ButtonSupport(R.id.Button05, 10000l,YourClassActivity8.class),
            new ButtonSupport(R.id.Button02, 11000l,YourClassActivity9.class),
    };

    private static class ButtonSupport{
        final int buttonId;
        final long duration;
        final Class<? extends Activity> clazz;

        ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
            this.buttonId = buttonId;
            this.duration = duration;
            this.clazz = clazz;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (ButtonSupport buttonSupport : buttonSupports) {
            animButton(buttonSupport);
        }

    }

    private void animButton(final ButtonSupport buttonSupport) {
        final Button button = (Button) findViewById(buttonSupport.buttonId);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startButtonAnimation(v, buttonSupport.clazz);
            }
        });
        Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
        anim.setDuration(buttonSupport.duration);
        button.startAnimation(anim);
    }

    public void startButtonAnimation(View btn, final Class<? extends  Activity> clazz) {
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        btn.setAnimation(shake);
        btn.startAnimation(shake);

        shake.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                player = MediaPlayer.create(MainActivity.this, R.raw.music);
                player.setLooping(true); // Set looping
                player.setVolume(1,1);
                player.start();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                player.stop();
                startActivity(new Intent(getApplicationContext(), clazz));
                overridePendingTransition(R.anim.animation, R.anim.animation2);
                playerp = MediaPlayer.create(MainActivity.this, R.raw.musicp);
                playerp.setLooping(false); // Set looping
                playerp.setVolume(1,1);
                playerp.start();


            }

        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

动画xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="1000"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>

1 个答案:

答案 0 :(得分:0)

我想最简单的改变就是这样:

int ANIM_OFFSET=100;

....

for (int i=0;i<buttonSupports.length;i++) {
    ButtonSupport buttonSupport =buttonSupports[i];
    animButton(buttonSupport,i);
}

....

anim.setStartOffset(i*ANIM_OFFSET);