简体   繁体   中英

animation on pressing back button

I have added the following code to my activity and got the desired animation, but pressing the back button the animation is not the same Ie the activity just closes normally. How can I add animation when back button is pressed

public void notesAndCodeClick(View v){

    Intent notesIntent = new Intent(MainActivity.this, NotesActivity.class);
    ActivityOptions notesoptions = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
    startActivity(notesIntent, notesoptions.toBundle());
}

Try this,

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        MainActivity.this.overridePendingTransition(R.anim.trans_right_in,
                R.anim.trans_right_out);
    }

Add both the files listed below to your anim folder

res --> anim

trans_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="-100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0" />

</set>

trans_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="100%p" />

</set>

You can set IN and OUT animation for Activity while pressing back button.

Left to Right animation :

Put this file in res/anim/left_to_right.xml :-

 <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
      <translate android:fromXDelta="-100%" android:toXDelta="0%"
                 android:fromYDelta="0%" android:toYDelta="0%"
                 android:duration="700"/>
    </set>

Right to Left animation :

Put this file in res/anim/right_to_left.xml :-

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

Now in onBackPressed() : -

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);   
}

You have to just do one thing, call animation after finishing your activity like this.

finish();
overridePendingTransition(R.anim.nothing,R.anim.nothing);

Happy Coding....

To add an animation when the back button is pressed, you can use the onBackPressed () method of the Activity class.

Example

@Override
public void onBackPressed() {
    super.onBackPressed();
    // add your animation   
}

Android documentation

public void onBackPressed (): Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM