Sajad Rahmanipour
Sajad Rahmanipour

Reputation: 395

Button Animation On Click And Go to Other Activity With An Other Animation

I need a code to do Button Animation On Click(like Rotation or quake or ..) And Go to Other Activity With An Other Animation After Button Animation Ended. can you help me?

I tried this code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button b=(Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
            b.startAnimation(anim);
            startActivity(new Intent(getApplicationContext(), Activity2.class));
            overridePendingTransition(R.anim.animation, R.anim.animation2);

but both animation started same time

Upvotes: 1

Views: 1914

Answers (3)

Rohit Goswami
Rohit Goswami

Reputation: 617

create animation file named shake.xml-

<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:duration="70"
    android:fromDegrees="-5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:interpolator="@android:anim/linear_interpolator"
    android:toDegrees="5" />
<translate
    android:fromXDelta="-10"
    android:toXDelta="10"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="70" />
</set>

Now in your main activity onCreate Block

b = (Button)findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

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

after closing your onCreate() create method like this..

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(), SecondActivity.class));
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        }
    });
}

For more help let me know....

Upvotes: 1

LostGeek
LostGeek

Reputation: 123

Rohit is right.

startActivity(new Intent(getApplicationContext(), ActivityTwo.class));
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);

from this link : http://chrisrisner.com/31-Days-of-Android--Day-17%E2%80%93Animating-between-Activities

Upvotes: 0

Fahim
Fahim

Reputation: 12388

Try in this manner. Move to next activity on animation finish

Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
        anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

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

            }
        });
        b.startAnimation(anim);

Upvotes: 0

Related Questions