How can I define the transition between two activities for Android 1.5 and later? I would like an activity to fade in.
问题:
回答1:
You can do this with Activity.overridePendingTransition()
. You can define simple transition animations in an XML resource file.
回答2:
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.
回答3:
An even easy way to do it is:
- 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>
- 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 :)
回答4:
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.
回答5:
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.
回答6:
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;
回答7:
Here\'s the code to do a nice smooth between two activity.
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>
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>
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();
回答8:
I overwrite my default activity animation. I test it in api 15 that it work smoothly. Here is the solution that I use:
<!-- Base application theme. -->
<style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.NoActionBar\">
<!-- Customize your theme here. -->
<item name=\"colorPrimary\">@color/colorPrimary</item>
<item name=\"colorPrimaryDark\">@color/colorPrimaryDark</item>
<item name=\"colorAccent\">@color/colorPrimary</item>
<item name=\"android:windowAnimationStyle\">@style/CustomActivityAnimation</item>
</style>
<style name=\"CustomActivityAnimation\" parent=\"@android:style/Animation.Activity\">
<item name=\"android:activityOpenEnterAnimation\">@anim/slide_in_right</item>
<item name=\"android:activityOpenExitAnimation\">@anim/slide_out_left</item>
<item name=\"android:activityCloseEnterAnimation\">@anim/slide_in_left</item>
<item name=\"android:activityCloseExitAnimation\">@anim/slide_out_right</item>
</style>
Create anim folder under res folder and then create this four animation files:
slide_in_right.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<set xmlns:android=\"http://schemas.android.com/apk/res/android\">
<translate android:fromXDelta=\"100%p\" android:toXDelta=\"0\"
android:duration=\"@android:integer/config_mediumAnimTime\"/>
</set>
slide_out_left.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<set xmlns:android=\"http://schemas.android.com/apk/res/android\">
<translate android:fromXDelta=\"0\" android:toXDelta=\"-100%p\"
android:duration=\"@android:integer/config_mediumAnimTime\"/>
</set>
slide_in_left.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<set xmlns:android=\"http://schemas.android.com/apk/res/android\">
<translate android:fromXDelta=\"-100%p\" android:toXDelta=\"0\"
android:duration=\"@android:integer/config_mediumAnimTime\"/>
</set>
slide_out_right.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<set xmlns:android=\"http://schemas.android.com/apk/res/android\">
<translate android:fromXDelta=\"0\" android:toXDelta=\"100%p\"
android:duration=\"@android:integer/config_mediumAnimTime\"/>
</set>
You can download my sample project.
That\'s all... :)
回答9:
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.
回答10:
IN GALAXY Devices :
You need to make sure that you havn\'t turned it off in the device using the Settings > Developer Options:
回答11:
Use ActivityCompat.startActivity() works API > 21.
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionImage, EXTRA_IMAGE);
ActivityCompat.startActivity(activity, intent, options.toBundle());
回答12:
Before Starting your Intent:
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(AlbumListActivity.this);
startActivity(intent, options.toBundle());
This gives Default Animation to your Activity Transition.
回答13:
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..
回答14:
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\" />