如何定义按钮显示逐个出现动画?

时间:2015-03-09 09:51:31

标签: android animation duration

我的活动中有9个按钮,我使用右推动画在Create上显示我的按钮,我改变持续时间来设置显示,所以我有9个animation.xml,持续时间不同(从1000到5000)。< / p>

我也必须在活动中重复3行代码,

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

1 个答案:

答案 0 :(得分:3)

是的,你可以创建方法

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

这将是最节省的

修改

请找到我的班级命题:

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