Je commence une activité et je préférerais avoir un fondu enchaîné en alpha pour startActivity()
et un fondu enchaîné pour le finish()
. Comment puis-je procéder dans le SDK Android ?
Réponses
Trop de publicités?Si vous voulez toujours avoir la même animation de transition pour l'activité
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
Vous pouvez simplement créer un contexte et faire quelque chose comme ci-dessous :-.
private Context context = this;
Et votre animation :-
((Activity) context).overridePendingTransition(R.anim.abc_slide_in_bottom,R.anim.abc_slide_out_bottom);
Vous pouvez utiliser toutes les animations que vous voulez.
Je voulais utiliser la solution styles.xml, mais elle n'a pas fonctionné pour moi avec les activités. Il s'avère qu'au lieu d'utiliser android:windowEnterAnimation
y android:windowExitAnimation
J'ai besoin d'utiliser les animations de l'activité comme ceci :
<style name="ActivityAnimation.Vertical" parent="">
<item name="android:activityOpenEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityOpenExitAnimation">@anim/exit_to_bottom</item>
<item name="android:activityCloseEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:activityCloseExitAnimation">@anim/exit_to_bottom</item>
<item name="android:windowEnterAnimation">@anim/enter_from_bottom</item>
<item name="android:windowExitAnimation">@anim/exit_to_bottom</item>
</style>
De plus, pour une raison quelconque, cela ne fonctionne qu'à partir d'Android 8 et plus. J'ai ajouté le code suivant à mon BaseActivity, afin de le corriger pour les niveaux d'API inférieurs :
override fun finish() {
super.finish()
setAnimationsFix()
}
/**
* The activityCloseExitAnimation and activityCloseEnterAnimation properties do not work correctly when applied from the theme.
* So in this fix, we retrieve them from the theme, and apply them.
* @suppress Incorrect warning: https://stackoverflow.com/a/36263900/1395437
*/
@SuppressLint("ResourceType")
private fun setAnimationsFix() {
// Retrieve the animations set in the theme applied to this activity in the manifest..
var activityStyle = theme.obtainStyledAttributes(intArrayOf(attr.windowAnimationStyle))
val windowAnimationStyleResId = activityStyle.getResourceId(0, 0)
activityStyle.recycle()
// Now retrieve the resource ids of the actual animations used in the animation style pointed to by
// the window animation resource id.
activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(activityCloseEnterAnimation, activityCloseExitAnimation))
val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
}
Voici le code pour faire un joli fondu entre deux activités
Créez un fichier appelé fadein.xml
en 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" />
Créez un fichier appelé fadeout.xml
en 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" />
Si vous voulez faire un fondu de l'activité A à l'activité B, mettez ce qui suit dans la méthode onCreate de l'activité B. Avant setContentView fonctionne pour moi.
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
Si les fondus sont trop lents pour vous, changez Android:duration dans les fichiers xml ci-dessus par quelque chose de plus petit.
Note : Réponse tirée de : Transition d'activité dans Android
// CREATE anim
// CREATE animation,animation2 xml // animation like fade out
Intent myIntent1 = new Intent(getApplicationContext(), Attend.class);
Bundle bndlanimation1 = ActivityOptions.makeCustomAnimation(getApplicationContext(),
R.anim.animation, R.anim.animation2).toBundle();
startActivity(myIntent1, bndlanimation1);
1 votes
stackoverflow.com/questions/8319465/
0 votes
Pour la transition des diapositives
startActivity(intent);
overridePendingTransition(android.R.anim.slide_out_right, android.R.anim.slide_in_left);