如何定义按钮显示与动画一起出现?

how to define button display to appear one by one with animation?

我的 activity 中有 9 个按钮,我使用右推动画在创建时显示我的按钮,我更改持续时间以设置显示,所以我有 9 个 animation.xml 不同的持续时间(来自前 1000 到 5000)。

我还必须在 activity 中重复 3 行代码 9 次,

final Button b = (Button)findViewById(R.id.Button06);
    Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
    b.startAnimation(anim);

所以,我想知道有没有更简单的方法可以用更少的代码制作类似的东西? 例如,使用 1 animation.xml 并将按钮定义为 运行 动画一个接一个?!

我的完整代码:

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button b = (Button)findViewById(R.id.Button06);
        Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
        b.startAnimation(anim);

        final Button b1 = (Button)findViewById(R.id.Button03);
        Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in1);
        b1.startAnimation(anim1);

        final Button b2 = (Button)findViewById(R.id.button1);
        Animation anim2=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in2);
        b2.startAnimation(anim2);

        final Button b3 = (Button)findViewById(R.id.Button08);
        Animation anim3=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in3);
        b3.startAnimation(anim3);

        final Button b4 = (Button)findViewById(R.id.Button04);
        Animation anim4=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in4);
        b4.startAnimation(anim4);

        final Button b5 = (Button)findViewById(R.id.Button01);
        Animation anim5=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in5);
        b5.startAnimation(anim5);

        final Button b6 = (Button)findViewById(R.id.Button07);
        Animation anim6=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in6);
        b6.startAnimation(anim6);

        final Button b7 = (Button)findViewById(R.id.Button05);
        Animation anim7=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in7);
        b7.startAnimation(anim7);

        final Button b8 = (Button)findViewById(R.id.Button02);
        Animation anim8=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in8);
        b8.startAnimation(anim8);


        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startButtonAnimation(b);
            }
        });

    }
    public void startButtonAnimation(Button btn){
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        btn.setAnimation(shake);
        btn.startAnimation(shake);

        shake.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent(getApplicationContext(), Activity2.class));
                overridePendingTransition(R.anim.animation, R.anim.animation2);
            }
        });
    }


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

是的,您可以创建方法

private void buttonAnim(int buttonId, long duration){
        Button b = (Button) findViewById(buttonId);
        Animation anim=AnimationUtils.loadAnimation(MainActivity.this,your_anim_id);
        anim.setDuration(duration);
        b.startAnimation(anim);
}

并用它来调用您的动画,例如:

buttonAnim(R.anim.Button06, 1000);

此外,您可以将按钮放入数组中并循环执行,如下面的代码所示:

int[] buttonsArrays = new int { 
                   R.anim.Button01,
                   R.anim.Button02,
                   R.anim.Button06,
                   ...
                   }

int[] durations = new int { 
                   1000,
                   5000,
                   1000,
                   ...
                   }

在您的代码中:

  for(int index=0; index <buttonsArrays.length; index++){
      buttonAnim(buttonsArrays[index],durations[index]); 
  }

但记住数组的长度必须相同

或者您可以使用 Pair 对象,如下面的代码所示:

    Pair<Integer, Long>[] pairs = new Pair[]{
            new Pair(R.id.Button01, 1000l),
            new Pair(R.id.Button02, 5000l),
            ...
    };

在您的代码中:

  for(Pair<Integer,Long> pair : pairs ){
      buttonAnim(pair.first,pair.second); 
  }

会最节省

编辑

请找出我对你的命题 class:

public class MainActivity extends ActionBarActivity {

    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, 4000l,YourClassActivity4.class),
            new ButtonSupport(R.id.Button04, 5000l,YourClassActivity5.class),
            new ButtonSupport(R.id.Button01, 6000l,YourClassActivity6.class),
            new ButtonSupport(R.id.Button07, 7000l,YourClassActivity7.class),
            new ButtonSupport(R.id.Button05, 8000l,YourClassActivity8.class),
            new ButtonSupport(R.id.Button02, 9000l,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.your_animation);
        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) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent(getApplicationContext(), clazz));
                overridePendingTransition(R.anim.animation, R.anim.animation2);
            }
        });
    }


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