J'essaye d'animer un ImageButton. Lorsque le bouton est cliqué, je veux qu'il glisse vers la droite. Lorsque vous cliquez à nouveau, il doit revenir à sa position d'origine. Lorsque l'animation se termine, elle revient à la position d'origine, donc je repositionne le bouton ImageButton. J'ai un petit "flash" où l'ImageButton et l'animation sont à la position originale avant que le ImageButton ait été repositionné. Quelqu'un peut-il me dire comment se débarrasser de ce "flash"?Animation de diapositives Android Clignotant
J'ai écrit le code suivant pour démarrer les animations et lorsque l'animation se termine, déplacez ImageButton lui-même. Qu'est-ce que c'est, est un onClickListener pour glisser le ImageButton dans et un AnimationListener pour cela. Il y a aussi un onClickListener et un AnimationListener pour faire glisser le ImageButton.
private class SceneIndicatorListenerIn implements ImageButton.OnClickListener {
ImageButton imageButton;
public SceneIndicatorListenerIn (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onClick(View view) {
// Create animation
Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_in);
anim.setAnimationListener(new SceneIndicatorListenerInDidEnd(imageButton));
view.startAnimation(anim);
}
}
private class SceneIndicatorListenerInDidEnd implements AnimationListener {
ImageButton imageButton;
public SceneIndicatorListenerInDidEnd (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onAnimationEnd(Animation anim) {
Log.d(LOG_TAG, "In animation did end");
// This is for density pixels
float dp = context.getResources().getDisplayMetrics().density;
// Keep position after animation
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
params.setMargins((int) (0 * dp), imageButton.getTop(), 0, 0);
imageButton.setLayoutParams(params);
// Change on click listener
imageButton.setOnClickListener(new SceneIndicatorListenerOut(imageButton));
}
@Override
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationStart(Animation arg0) {}
}
private class SceneIndicatorListenerOut implements ImageButton.OnClickListener {
ImageButton imageButton;
public SceneIndicatorListenerOut (ImageButton imageButton) {
Log.d(LOG_TAG, "My imageButton was set");
this.imageButton = imageButton;
}
@Override
public void onClick(View view) {
Log.d(LOG_TAG, "You clicked me");
// Create animation
Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_out);
anim.setAnimationListener(new SceneIndicatorListenerOutDidEnd(imageButton));
view.startAnimation(anim);
}
}
private class SceneIndicatorListenerOutDidEnd implements AnimationListener {
ImageButton imageButton;
public SceneIndicatorListenerOutDidEnd (ImageButton imageButton) {
this.imageButton = imageButton;
}
@Override
public void onAnimationEnd(Animation anim) {
Log.d(LOG_TAG, "Out animation did end");
// This is for density pixels
float dp = context.getResources().getDisplayMetrics().density;
// Keep position after animation
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
params.setMargins((int) (-199 * dp), imageButton.getTop(), 0, 0);
imageButton.setLayoutParams(params);
// Change on click listener
imageButton.setOnClickListener(new SceneIndicatorListenerIn(imageButton));
}
@Override
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationStart(Animation arg0) {}
}
Ceci est mon animation pour glisser dans:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0%"
android:toXDelta="83%"
android:duration="750" />
</set>
Et ceci est mon animation pour le dos coulissant:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0%"
android:toXDelta="-80%"
android:duration="750" />
</set>