如何在动画结束后仅关闭弹出窗口窗口



我知道存在与此问题类似的主题,但我不知道如何使用我的代码使其工作。

有一个弹出窗口,(我什至不知道这是否是做我想做的事的最佳方式,但我在 Android 上是新手),但是我已经做了一个弹出窗口,在我阅读了关于如何对弹出窗口进行动画处理的问题后,单击带有我在这里找到的带有动画的图像时会出现弹出窗口,并且我必须做进入和退出的动画, 我想怎么做,但现在我的问题:

我不知道在输入动画结束后如何关闭弹出窗口。

弹出窗口代码:

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>

我想在进入动画结束后关闭弹出窗口,我的目标是弹出窗口出现,移动到顶部一点点并消散

您可以使用动画侦听器。

您必须在动画变量中定义动画,如下所示

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

布鲁诺,

面临同样的问题。不幸的是,目前无法获得弹出窗口中使用的动画的回调。我使用计数停机时间做了一个小的解决方法。不完美 - 但效果很好。希望对您有所帮助。

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

最新更新