2017-01-26 29 views
0

我知道存在一個類似於這個問題的主題,但我無法確定如何使用我的代碼工作。我有一個popupwindow,(我甚至不知道這是否是最好的方式來做我想做的事,但在Android上我是新來的),但是我做了一個popupWindow出現在點擊圖片上,在這裏,我讀了關於如何動畫popupwindow和問題後,我有待辦事項的動畫進入和退出,我想,該怎麼做,但現在我的問題:如何在動畫結束後關閉popupWindow

我不知道該怎麼辦在輸入動畫結束後,popupwindow關閉。

popupWindow代碼:

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View viewAnimationResourceEarned = inflater.inflate(R.layout.layout_animation_resources_earned, null); 

animationResourceEarned = new PopupWindow(viewAnimationResourceEarned, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
animationResourceEarned.setAnimationStyle(R.style.styleAnimationResourceEarned); 
animationResourceEarned.showAtLocation(viewAnimationResourceEarned, Gravity.CENTER, 0, 0); 

輸入動畫代碼:

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

    <scale xmlns:android="http://schemas.android.com/apk/res/android" 
     android:toXScale="1.0" 
     android:fromXScale="0.0" 

     android:toYScale="1.0" 
     android:fromYScale="0.0" 

     android:pivotX="0%" 
     android:pivotY="50%" 

     android:startOffset="100" 
     android:duration="300" /> 

    <translate 
     android:fromYDelta="0" 
     android:toYDelta="-50" 
     android:duration="2500"/> 
</set> 

我要取消的進入動畫結束後,在彈出的窗口,我的目標是popupappear,移動到頂部痘痘和diseppear

回答

0

您可以使用動畫偵聽器。

您必須定義動畫的動畫變量這樣

Animation animation = AnimationUtils.loadAnimation(this, R.style.styleAnimationResourceEarned); 

然後,

animation.setAnimationListener(new Animation.AnimationListener() { 

@Override public void onAnimationStart(Animation animation) { 

} 

@Override public void onAnimationEnd(Animation animation) { 
//Dismiss it 
} 

@Override public void onAnimationRepeat(Animation animation) { 

} }); 
+0

我把 「動漫動畫=新的動畫(0,R.style.styleAnimationResourceEarned);」在我的代碼上,但動畫是抽象的;無法實例化 – BrunoM24

+0

我犯了一個錯誤。我編輯了我的答案。請立即檢查。 –

+0

不工作,因爲「styleAnimationResourceEarned」不是資源類型動畫,我嘗試加載進入的動畫,但在popupWindow中導致我需要設置動畫風格,偵聽器不檢測動畫 – BrunoM24

0

布魯諾,

面臨着同樣的問題。不幸的是,目前還沒有辦法讓popupwindow中使用的動畫回調。我使用countdowntimer做了一個小的解決方法。 不完美 - 但效果很好。希望能幫助到你。

popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 
     @Override 
     public void onDismiss() { 
      new CountDownTimer(animationDuration, 10) { 
       public void onTick(long millisUntilFinished) { 
       } 
       public void onFinish() { 
        toggle(); 
       } 
      }.start(); 
     } 
    }); 

另外擺脫資源文件中使用動畫持續時間下面的代碼

int[] attrs = {android.R.attr.windowExitAnimation}; 
    TypedArray ta = context.obtainStyledAttributes(R.style.PopUpMenuAnimation, attrs); 
    int resID = ta.getResourceId(0,0); 
    Animation anim = AnimationUtils.loadAnimation(context, resID); 
    final long animationDuration = anim.getDuration(); 
相關問題