=====Généralités===== ====Fonctionnalités==== ===Interpolation=== Liste des interpolations avec leur courbe : [[https://jebware.com/interp/android-animation-interpolators.html|Android Animation Interpolators]] {{ :lang:android:animation:android_animation_interpolators_29_05_2023_09_31_25_.html |Archive du 2014 le 29/05/2023}} Il existe : * ''AccelerateDecelerateInterpolator'', * ''AccelerateInterpolator'', * ''AnticipateInterpolator'', * ''AnticipateOvershootInterpolator'', * ''BounceInterpolator'', * ''CycleInterpolator'', * ''DecelerateInterpolator'', * ''LinearInterpolator'', * ''OvershootInterpolator'', * ''PathInterpolator''. ====Comparaison Animation / Animator==== Pour faire des animations, il est possible d'utiliser des classes ''Animation'' et des ''Animator''. Différence entre les deux, en mettant en avant ''Animator''. + Plus récent. + Composant graphique utilisable pendant l'animation. + Permet de synchroniser plusieurs animations sur plusieurs composants graphiques. + L'implémentation des ''listener'' se fait sur la base d'une classe abstraite et non d'une interface ce qui évite de devoir définir tous les événements. + Permet d'enchainer des animations complexes dans la ressource XML. + Peuvent s'utiliser pour créer des ''animated-vector''. - Ne peut s'appliquer qu'à un seul composant graphique à la fois. Sinon, il faut multiplier les instances ''Animator''. - Nécessite plus de ressource. - Ne permet pas de déplacer d'un pourcentage de la taille du composant depuis la ressource XML. Nécessite Java. - Nécessite plus de ligne de code. - Ne peut pas être créé depuis du code Java (sauf animation très simple). Nécessite d'avoir une ressource XML. - Animation difficilement personnalisable en Java. Globalement, ''Animator'' est mieux. [[https://stackoverflow.com/questions/28220613/what-is-the-difference-between-an-animator-and-an-animation|What is the difference between an Animator and an Animation?]] {{ :lang:android:animation:android_-_what_is_the_difference_between_an_animator_and_an_animation_-_stack_overflow_29_05_2023_08_57_53_.html |Archive du 29/01/2025 le 29/05/2023}} =====Animation===== ====Ressources==== Les ressources XML se mettent dans le dossier ''res/anim''. ====Exemples==== Animation animation = AnimationUtils.loadAnimation(context, R.anim.animation); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationEnd(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} }); Button button = myView.findViewById(R.id.button); button.startAnimation(buttonIn); Ici, animation est un ''AnimatorSet'' qui contient 2 animations (''AlphaAnimation'' et ''TranslateAnimation''). =====Animator===== ====Ressources==== * Plusieurs animations simples à la suite * Faire zoomer une image svg Attention, il faut que les deux images svg soient compatibles (mêmes nombres de points). * Image animée L'animator. L'image. L'image animée. ====Exemples==== * Minimaliste Animator animator = AnimatorInflater.loadAnimator(context, R.animator.animator); Button button = myView.findViewById(R.id.button); animator.setTarget(button); * Modifier une valeur d'un champ. Il n'existe pas de méthode générique. Il faudra que l'animation XML soit parfaitement cohérente avec le code Java. ((ValueAnimator)((AnimatorSet) animator).getChildAnimations().get(0)).getValues()[0].setFloatValues(0.1f); * Piloter plusieurs animations AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(anim1, anim2); animatorSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { callback.run(); } }); animatorSet.start(); Attention, si une animation a un ''StartDelay'' et pas les autres, il faudra faire attention à l'ordre (lequel?) dans la méthode ''playTogether''.
Note that an Animator with a startDelay will not actually start until that delay elapses, which means that if the first animator in the list supplied to this constructor has a startDelay, none of the other animators will start until that first animator's startDelay has elapsed.[[https://developer.android.com/reference/androidx/core/animation/AnimatorSet|AnimatorSet]] {{ :lang:android:animation:animatorset_android_developers_29_05_2023_11_28_29_.html |Archive du 08/03/2023 le 29/05/2023}}
=====Limitations===== * SurfaceView / GLSurfaceView Il n'est pas possible d'appliquer une animation d'apparition et de disparition sur les composants ''SurfaceView''. La solution est d'appliquer une surface au dessus de la ''SufaceView'' ayant comme couleur celle d'arrière-plan. Et donc, pour faire apparaitre progressivement la ''SurfaceView'', il suffit de faire disparaitre progressivement la surface dessus. [[https://stackoverflow.com/questions/11801997/alphaanimation-over-surfaceview|AlphaAnimation over SurfaceView?]] {{ :lang:android:animation:android_-_alphaanimation_over_surfaceview_-_stack_overflow_23_08_2023_11_04_08_.html |Archive du 03/08/2011 le 23/08/2023}}