Android中的Activity转换

如何为Android 1.5及更高版本定义两个活动之间的转换? 我想淡入一个活动。

267976 次浏览

You can do this with Activity.overridePendingTransition(). You can define simple transition animations in an XML resource file.

Yes. You can tell the OS what kind of transition you want to have for your activity.

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setWindowAnimations(ANIMATION);


...


}

Where ANIMATION is an integer referring to a built in animation in the OS.

You cannot use overridePendingTransition in Android 1.5. overridePendingTransistion came to Android 2.0.

If you're gonna go through this without any error you have to compile for the target (1.5 or higher) using the ordinary animations (or you own) or you have to compile for the target (2.0 or higher) using overridePendingTransistion.

Summary: You cannot use overridePendingTransistion in Android 1.5.

You can though use the built-in animations in the OS.

Here's the code to do a nice smooth fade between two Activities..

Create a file called fadein.xml in res/anim

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />

Create a file called fadeout.xml in res/anim

<?xml version="1.0" encoding="utf-8"?>


<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />

If you want to fade from Activity A to Activity B, put the following in the onCreate() method for Activity B. Before setContentView() works for me.

overridePendingTransition(R.anim.fadein, R.anim.fadeout);

If the fades are too slow for you, change android:duration in the xml files above to something smaller.

For a list of default animations see: http://developer.android.com/reference/android/R.anim.html

There is in fact fade_in and fade_out for API level 1 and up.

create res>anim>fadein.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />

create res>anim>fadeout.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />

In res>values>styles.xml

<style name="Fade">
<item name="android:windowEnterAnimation">@anim/fadein</item>
<item name="android:windowExitAnimation">@anim/fadeout</item>
</style>

In activities onCreate()

getWindow().getAttributes().windowAnimations = R.style.Fade;

An even easy way to do it is:

  1. Create an animation style into your styles.xml file
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
  1. Add this style to your app theme
<style name="AppBaseTheme" parent="Theme.Material.Light.DarkActionBar">
<item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>

That's it :)

IN GALAXY Devices :

You need to make sure that you havn't turned it off in the device using the Settings > Developer Options:

two muppets

Here's the code to do a nice smooth between two activity.

  1. smooth effect from left to right

    Create a file called slide_in_right.xml and slide_out_right.xml in res/anim

    slide_in_right.xml

        <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
    </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
    <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
    </set>
    
  2. smooth effect from right to left

    Create a file called animation_enter.xml and animation_leave.xml in res/anim

    animation_enter.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>
    

    animation_leave.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>
    
  3. Navigate from one activity to second Activity

       Intent intent_next=new Intent(One_Activity.this,Second_Activity.class);
    overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
    startActivity(intent_next);
    finish();
    

    4.On back press event or Navigate from second activity to one Activity

     Intent home_intent = new Intent(Second_Activity.this, One_Activity.class);
    overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
    startActivity(home_intent);
    finish();
    

Use ActivityCompat.startActivity() works API > 21.

    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionImage, EXTRA_IMAGE);
ActivityCompat.startActivity(activity, intent, options.toBundle());

Before Starting your Intent:

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(AlbumListActivity.this);
startActivity(intent, options.toBundle());

This gives Default Animation to your Activity Transition.

Some versions of Android support custom Activity transitions and some don't (older devices). If you want to use custom transitions it's good practice to check whether the Activity has the overridePendingTransition() method, as on older versions it does not.

To know whether the method exists or not, reflection API can be used. Here is the simple code which will check and return the method if it exists:

Method mOverridePendingTransition;


try {
mOverridePendingTransition = Activity.class.getMethod(
"overridePendingTransition", new Class[] { Integer.TYPE, Integer.TYPE } );
/* success */
} catch (NoSuchMethodException nsme) {
/* failure, this version of Android doesn't have this method */
}

And then, we can apply our own transition, i.e. use this method if it exists:

if (UIConstants.mOverridePendingTransition != null) {
try {
UIConstants.mOverridePendingTransition.invoke(MainActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

Here, as an example, simple fade-in and fade-out animations were used for transition demonstration..

zoom in out animation

Intent i = new Intent(getApplicationContext(), LoginActivity.class);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
startActivity(i);
finish();

zoom_enter

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />

zoom_exit

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />