【问题标题】:How to only dismiss a popupWindow after animation end如何仅在动画结束后关闭弹出窗口
【发布时间】:2017-01-26 18:35:35
【问题描述】:

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

我有一个弹出窗口,(我什至不知道这是否是做我想做的最好的方法,但我是 Android 上的新手),但我已经完成了一个弹出窗口出现在点击带有动画的图像时,我在这里找到了动画,在我阅读了一个关于如何为弹出窗口设置动画的问题并且我必须执行 Enter 和 Exit 的动画之后,我想出了如何做到这一点,但现在我的问题是:

Enter Animatio结束后我不知道如何关闭弹出窗口。

弹出窗口代码:

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>

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

【问题讨论】:

    标签: android android-layout android-studio android-animation


    【解决方案1】:

    您可以使用动画监听器。

    你必须像这样在动画变量中定义动画

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

    【讨论】:

    • 我把“Animation animation = new Animation (0,R.style.styleAnimationResourceEarned);”在我的代码上,但动画是抽象的;无法实例化
    • 我犯了一个错误。我已经编辑了我的答案。请立即检查。
    • 不工作,因为“styleAnimationResourceEarned”不是资源类型动画,我尝试加载进入的动画,但在popupWindow中我需要设置动画样式,监听器没有检测到动画
    【解决方案2】:

    布鲁诺,

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      相关资源
      最近更新 更多