Outils pour utilisateurs

Outils du site


lang:android:animation

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
lang:android:animation [2023/05/29 10:18] – Création avec "Animation" rootlang:android:animation [2023/08/23 11:04] (Version actuelle) – Ajout de "SurfaceView" root
Ligne 34: Ligne 34:
  
 + Permet d'enchainer des animations complexes dans la ressource XML. + 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. - Nécessite plus de ressource.
Ligne 70: Ligne 74:
 </code> </code>
  
-====Exemple====+====Exemples====
  
 <code java> <code java>
Ligne 89: Ligne 93:
 </code> </code>
  
 +Ici, animation est un ''AnimatorSet'' qui contient 2 animations (''AlphaAnimation'' et ''TranslateAnimation'').
 +
 +=====Animator=====
 +
 +====Ressources====
 +
 +  * Plusieurs animations simples à la suite
 +
 +<code xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<set xmlns:android="http://schemas.android.com/apk/res/android"
 +  android:ordering="sequentially">
 +  <!-- wait -->
 +  <objectAnimator android:duration="2350">
 +    <propertyValuesHolder
 +      android:propertyName="alpha"
 +      android:valueFrom="0f"
 +      android:valueTo="0f"
 +      android:valueType="floatType" />
 +  </objectAnimator>
 +  <objectAnimator
 +    android:duration="750"
 +    android:interpolator="@android:anim/accelerate_interpolator">
 +    <propertyValuesHolder
 +      android:propertyName="translationY"
 +      android:valueFrom="250dp"
 +      android:valueTo="0dp" />
 +    <propertyValuesHolder
 +      android:propertyName="translationX"
 +      android:valueFrom="40dp"
 +      android:valueTo="0dp" />
 +    <propertyValuesHolder
 +      android:propertyName="alpha"
 +      android:valueFrom="0f"
 +      android:valueTo="1f"
 +      android:valueType="floatType" />
 +  </objectAnimator>
 +</set>
 +</code>
 +
 +  * Faire zoomer une image svg
 +
 +Attention, il faut que les deux images svg soient compatibles (mêmes nombres de points).
 +
 +<code xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<set xmlns:android="http://schemas.android.com/apk/res/android">
 +  <objectAnimator
 +    android:duration="1000"
 +    android:interpolator="@android:anim/bounce_interpolator"
 +    android:propertyName="pathData"
 +    android:valueFrom="M 0 125 A 10 10 90 0 1 250 125 A 10 10 90 0 1 0 125 z"
 +    android:valueTo="M 40 125 A 10 10 90 0 1 210 125 A 10 10 90 0 1 40 125 z"
 +    android:valueType="pathType" />
 +</set>
 +</code>
 +
 +  * Image animée
 +
 +L'animator.
 +
 +<code xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<set xmlns:android="http://schemas.android.com/apk/res/android">
 +  <objectAnimator
 +    android:duration="2000"
 +    android:propertyName="fillAlpha"
 +    android:startOffset="0"
 +    android:valueFrom="0.3f"
 +    android:valueTo="1.0f"
 +    android:valueType="floatType" />
 +</set>
 +</code>
 +
 +L'image.
 +
 +<code xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<vector xmlns:android="http://schemas.android.com/apk/res/android"
 +  android:width="100dp"
 +  android:height="200dp"
 +  android:viewportWidth="180"
 +  android:viewportHeight="360">
 +
 +  <path
 +    android:name="path"
 +    android:fillAlpha="0.3"
 +    android:fillColor="@color/md_theme_dark_onBackground"
 +    android:pathData="M 80 37.5 A 1 1 10 0 1 100 37.5 A 1 1 10 0 1 80 37.5 z" />
 +
 +</vector>
 +</code>
 +
 +L'image animée.
 +
 +<code xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
 +  android:drawable="@drawable/image">
 +
 +  <target
 +    android:name="path"
 +    android:animation="@animator/animator" />
 +</animated-vector>
 +</code>
 +
 +====Exemples====
 +
 +  * Minimaliste
 +
 +<code java>
 +Animator animator = AnimatorInflater.loadAnimator(context, R.animator.animator);
 +Button button = myView.findViewById(R.id.button);
 +animator.setTarget(button);
 +</code>
 +
 +  * 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.
 +
 +<code java>
 +((ValueAnimator)((AnimatorSet) animator).getChildAnimations().get(0)).getValues()[0].setFloatValues(0.1f);
 +</code>
 +
 +  * Piloter plusieurs animations
 +
 +<code java>
 +AnimatorSet animatorSet = new AnimatorSet();
 +animatorSet.playTogether(anim1, anim2);
 +animatorSet.addListener(new AnimatorListenerAdapter() {
 +  @Override
 +  public void onAnimationEnd(Animator animation) {
 +    callback.run();
 +  }
 +});
 +animatorSet.start();
 +</code>
 +
 +Attention, si une animation a un ''StartDelay'' et pas les autres, il faudra faire attention à l'ordre (lequel?) dans la méthode ''playTogether''.
 +
 +<blockquote>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.<cite>[[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}}</cite></blockquote>
 +
 +=====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}}
lang/android/animation.1685348316.txt.gz · Dernière modification : 2023/05/29 10:18 de root