2012-02-12 72 views
32

彈出窗口的動畫我在我的應用程序哈瓦一個彈出窗口,它的出現在某些按鈕點擊 我想動畫淡入設置到這個窗口, 我把XML文件中的「RES /阿尼姆「文件夾並設置彈出窗口的動畫樣式,但動畫不起作用? 這裏是我的代碼:如何使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> 

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

創建彈出窗口

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;} 
} 

=============== ================================== 的onClick方法...(optionsPopup是類型PopupWindow的全局變量)

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

回答

86

我覺得問題是你只提供了一組動畫風格。但實際上,一個PopupWindow需要兩個動畫。顯示窗口時將使用其中一個,隱藏窗口時使用其中一個。

這是你應該怎麼做,

1)創建兩套不同的動畫。

說,popup_show.xmlpopup_hide.xml,並把它添加到你不得不在屋裏資源文件夾中創建您的阿尼姆文件夾。

2)現在裏面文件夾中創建一個名爲styles.xml xml和這些動畫添加到像這樣,

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

3)現在設置這種風格你PopupWindow動畫,

popup.setAnimationStyle(R.style.Animation); 

現在,它會自動檢測窗口進入和退出,並使用所需的動畫提供。

+0

這有一定showaslocation方法調用之前設置。但不知何故,當我單擊按鈕時,彈出窗口會多次打開,任何線索爲什麼會多次? – Ari 2016-07-07 16:24:15

+0

@Ari你解決了雙動畫的問題嗎? – Anton 2016-10-19 19:44:50

+0

我設法通過從動畫中移除android:repeatCount =「1」來擺脫雙重動畫 – NewestStackOverflowUser 2016-11-25 23:39:05

11

我使用彈出動畫與此代碼:

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

位於/res/animator/popupanim.xml (popupanim.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="500" 
     android:repeatCount="0"/> 
</set> 
5

這可能是有點晚了,但爲什麼動畫沒有顯示的原因是因爲你是顯示您設置動畫之前的popupwindow。

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

反轉兩條線,你會看到動畫:

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