plus flexible (et assez facile) façon de créer une animation personnalisée est d'étendre Animation
classe.
En général:
- Régler la durée de votre méthode animation à l'aide
setDuration()
.
- option définir la méthode interpolateur pour votre animation à l'aide
setInterpolator()
(pour exapmle vous pouvez utiliser LinearInterpolator
ou AccelerateInterpolator
etc.)
- Remplacer
applyTransformation
. Ici nous nous intéressons à la variable interpolatedTime
qui change entre 0.0 et 1.0 et représente la progression de votre animation.
Voici un exemple (j'utilise cette classe pour changer ofsset de mon Bitmap
Bitmap
est lui-même tiré dans la méthode draw
.): Aussi
public class SlideAnimation extends Animation {
private static final float SPEED = 0.5f;
private float mStart;
private float mEnd;
public SlideAnimation(float fromX, float toX) {
mStart = fromX;
mEnd = toX;
setInterpolator(new LinearInterpolator());
float duration = Math.abs(mEnd - mStart)/SPEED;
setDuration((long) duration);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float offset = (mEnd - mStart) * interpolatedTime + mStart;
mOffset = (int) offset;
postInvalidate();
}
}
vous pouvez modifier View
en utilisant Transformation#getMatrix()
.
MISE À JOUR
En cas si vous utilisez Android cadre Animator (ou la mise en œuvre de compatibilité - NineOldAndroids
) vous pouvez simplement déclarer setter et getter pour votre propriété personnalisée View
et animer directement. Voici un autre exemple:
public class MyView extends View {
private int propertyName = 50;
/* your code */
public int getPropertyName() {
return propertyName;
}
public void setPropertyName(int propertyName) {
this.propertyName = propertyName;
}
/*
There is no need to declare method for your animation, you
can, of course, freely do it outside of this class. I'm including code
here just for simplicity of answer.
*/
public void animateProperty() {
ObjectAnimator.ofInt(this, "propertyName", 123).start();
}
}
[l'animation dans Android] (http://www.singhajit.com/android-custom-animations/) –
@AjitSingh, cet article décrit les animations standards (rotation, translation, etc.). Ce que je demande à propos ici est des animations personnalisées. – aioobe