2012-02-12 20 views
32

Ho una finestra popup nella mia applicazione, appare quando si fa clic su un pulsante Voglio impostare l'animazione di dissolvenza in questa finestra, Ho messo il file xml in "res/anim "cartella e impostare lo stile di animazione per la finestra popup ma l'animazione non funziona? Ecco il mio codice:come creare un'animazione per finestra popup in 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> 

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

creare la finestra 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;} 
} 

=============== ================================== metodo onClick ... (optionsPopup è una variabile globale di tipo PopupWindow)

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

risposta

86

Penso che il problema è che hai fornito solo un set di stile di animazione. Ma in realtà uno PopupWindow richiede due animazioni. Uno sarà usato da esso quando viene mostrata la finestra e un altro per nascondere la finestra.

Questo è come si dovrebbe fare,

1) Creare due diversi set di animazioni.

dicono, popup_show.xml e popup_hide.xml e aggiungerlo alla vostra cartella dianim che è necessario creare all'interno res cartella.

2) Ora all'interno valori cartella creare un XML chiamato styles.xml e aggiungere queste animazioni ad esso come questo,

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

3) Ora, impostare questo stile al vostro PopupWindow animazione,

popup.setAnimationStyle(R.style.Animation); 

Ora rileva automaticamente la finestra Invio ed Esci e fornisce l'animazione richiesta.

+0

Questo deve impostare prima di metodo showaslocation chiamata sicuramente.Ma in qualche modo Il popup si apre più volte ogni volta che clicco sul pulsante, qualche indizio perché più volte? – Ari

+0

@Ari hai risolto un problema con la doppia animazione? – Anton

+0

Sono riuscito a sbarazzarmi della doppia animazione rimuovendo android: repeatCount = "1" dall'animazione – NewestStackOverflowUser

11

sto utilizzando un'animazione popup con questo codice:

// 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); 

situato in /res/animator/popupanim.xml (popupanim.xml) il codice di animazione è:

<?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

Potrebbe essere un po 'tardi ma il motivo per cui l'animazione non è stata mostrata è perché stai mostrando la finestra popup prima di configurare l'animazione.

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

invertire i due righe e vedrete l'animazione:

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