New Android 4.0 Screen Transitions Between Activit

2020-07-26 11:41发布

问题:

I have been developing an Android app for a few months now and started testing on android 4.0. I just noticed that they changed the default transition between screens isn't that slide old off new on transition but instead some kind of popup the new from background like in iPhone.

Does anyone have any more info on the transition, is it configurable? If so where is it in the API I couldn't seem to find it.

回答1:

Use this to control the screen transition in android: overridePendingTransition

  • You should immediately call this after startActivity(intent);
  • Overrides onBackPressed(), startActivity(), and finish() on any activities that you wish to in control with.
  • It works in android version > 2.1.

Animating slide in/out:

Create following xml file in res/anim:

slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p" android:toXDelta="0"
    android:duration="@android:integer/config_shortAnimTime"/>

slide_in_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-100%p" android:toXDelta="0"
    android:duration="@android:integer/config_shortAnimTime"/>

slide_out_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:toXDelta="-100%p"
    android:duration="@android:integer/config_shortAnimTime" />

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:toXDelta="100%p"
    android:duration="@android:integer/config_shortAnimTime" />

The code snippet to slide from A->B:

public class A extends Activity {

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

@Override
public void startActivity(Intent intent) {
    super.startActivity(intent);
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);   
}

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);   

}


} 

public class B extends Activity {

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

@Override
public void startActivity(Intent intent) {
    super.startActivity(intent);
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left); 
}

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left); 

}


}