Sajad Rahmanipour
Sajad Rahmanipour

Reputation: 395

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

i have 9 buttons in my activity, i use push right animation to show my buttons on Create, i change duration to set display, so i have 9 animation.xml with diffrent duration (from 1000 to 5000 for ex).

also i have to repeat 3 line code for 9 time in activity ,

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

so, i need to know is there any easier way to make something like that with Less code? for ex, use 1 animation.xml and define button to run animation One after another?!

my full code:

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

Upvotes: 2

Views: 1032

Answers (1)

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

Yes you can create method

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

And use it to call your animation for ex:

buttonAnim(R.anim.Button06, 1000);

Additionally you can put your buttons in array and do it in loop as is shown in the code below:

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

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

And in your code:

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

But remember the lengths of arrays have to be the same

Or you can use Pair object as is shown in the code below:

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

And in your code:

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

It will be most save

EDIT

Please find my proposition of your 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);
    }
}

Upvotes: 3

Related Questions