2012-02-12 1 views
32

J'ai une fenêtre popup dans mon application, il apparaît quand un bouton cliqué Je veux mettre en fondu animation dans cette fenêtre, J'ai mis le fichier xml dans "res/anim "dossier et définir le style d'animation pour la fenêtre contextuelle, mais l'animation ne fonctionne pas? voici mes codes:comment faire une animation pour la fenêtre popup dans android

myanim.xml ...

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:duration="4000" 
     android:repeatCount="1"/> 
</set> 

=========================== ====================

Créer la fenêtre pop-up

private PopupWindow showOptions(Context mcon){ 
    try{ 
     LayoutInflater inflater = (LayoutInflater) mcon.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.options_layout,null); 
     layout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.myanim)); 
     PopupWindow optionspu = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     optionspu.setFocusable(true); 
     optionspu.showAtLocation(layout, Gravity.TOP, 0, 0); 
     optionspu.update(0, 0, LayoutParams.WRAP_CONTENT, (int)(hei/5)); 
     optionspu.setAnimationStyle(R.anim.myanim); 
     return optionspu; 
    } 
    catch (Exception e){e.printStackTrace(); 
    return null;} 
} 

=============== ===================================== Méthode onClick ... (optionsPopup est une variable globale de type PopupWindow)

@Override 
public void onClick(View v) { 
       switch (v.getId()) { 
     case R.id.options: 
       optionsPopup=showOptions(this); 
      break; 
} 

Répondre

86

Je pense que le problème est que vous avez fourni un seul jeu de style d'animation. Mais en réalité, un PopupWindow nécessite deux animations. Un sera utilisé par lui lorsque la fenêtre est affichée et l'autre pour cacher la fenêtre.

Voici comment vous devriez le faire,

1) Créer deux différents ensemble d'animations.

dire, popup_show.xml et popup_hide.xml et l'ajouter à votre dossier anim que vous devez créer à l'intérieur res dossier.

2) maintenant à l'intérieur valeurs créer un dossier xml appelé styles.xml et ajoutez ces animations comme ça,

<style name="Animation"> 
    <item name="android:windowEnterAnimation">@anim/popup_show</item> 
    <item name="android:windowExitAnimation">@anim/popup_hide</item> 
</style> 

3) Réglez maintenant ce style à votre animation PopupWindow,

popup.setAnimationStyle(R.style.Animation); 

Maintenant, il détecte automatiquement les fenêtres Enter et Exit et fournit l'animation requise.

+0

Cela doit mettre en avant la méthode showaslocation appel sûrement.Mais d'une manière ou d'une autre, le popup s'ouvre plusieurs fois quand je clique sur le bouton, des indices pourquoi plusieurs fois? – Ari

+0

@Ari avez-vous résolu un problème avec une animation double? – Anton

+0

J'ai réussi à me débarrasser de la double animation en supprimant android: repeatCount = "1" de l'animation – NewestStackOverflowUser

11

J'utilise une animation popup avec ce code:

// Creating the PopupWindow 
     layoutInflater = (LayoutInflater)  getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     inflatedLayoutView = layoutInflater.inflate(R.layout.packages_popup,null); 
    inflatedLayoutView.setAnimation(AnimationUtils.loadAnimation(this, R.animator.popupanim) 


    popup_l = new PopupWindow(inflatedLayoutView); 

    popup_l.setWidth(FrameLayout.LayoutParams.WRAP_CONTENT); 
    popup_l.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);  
    popup_l.setFocusable(true); 
    // Clear the default translucent background 
    popup_l.setBackgroundDrawable(new BitmapDrawable());  

    popup_l.showAtLocation(parent, Gravity.CENTER, 0 , 0); 

    popup_l.setOutsideTouchable(false); 

situé dans /res/animator/popupanim.xml (popupanim.xml) le code d'animation est:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

<alpha android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:duration="500" 
     android:repeatCount="0"/> 
</set> 
5

Cela peut être un peu tard, mais la raison pour laquelle l'animation n'a pas été affichée est parce que vous affichez la fenêtre popup avant de configurer votre animation.

optionspu.showAtLocation(layout, Gravity.TOP, 0, 0); 
optionspu.setAnimationStyle(R.anim.myanim); 

Inverser les deux lignes et vous verrez l'animation:

optionspu.setAnimationStyle(R.anim.myanim); 
optionspu.showAtLocation(layout, Gravity.TOP, 0, 0); 
Questions connexes