2011-10-12 3 views
3

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> 

Répondre

0

J'ai eu ce même problème et je voulais poster la réponse pour quiconque rencontre cela. Il y a un bug qui cause cela, peu importe ce que vous avez défini dans xml ou autre. La façon dont je l'ai contourné était de placer manuellement l'emplacement X hors de l'écran avant l'animation afin que le flash se produise hors de l'écran, puis il serait remis à sa position d'origine comme étant traité par le animation.

image.setX(-5000);//Really any number off of the screen will do 

Animation animation0 = AnimationUtils.loadAnimation(this, 
      R.anim.slide_across_right); 
animation0.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      animationHack(); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
     } 
}); 



private void animationHack() { 
    (new Handler()).postDelayed(new Runnable() { 
     public void run() { 
      image.setX(0); 
     } 
    }, 20); 

} 
Questions connexes