滚动时取消动画

时间:2014-10-16 15:25:00

标签: android android-animation android-popupwindow

我在列表视图上的按钮上实现了一个小动画。 基本上它就像添加/刷新列表quick action button一样。

我想要做的是在用户点击按钮并显示popup后为其设置动画。一旦popup被解除,该按钮应该回到其初始状态。

我已经制作了以下代码:

    final PopupWindow popup = new PopupWindow(getContext());

    // Attach layout
    LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.core_popup_quickactions, new LinearLayout(getContext()));

    // Creating the PopupWindow
    popup.setContentView(layout);
    popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popup.setOutsideTouchable(true);
    popup.setFocusable(true);
    popup.setBackgroundDrawable(new BitmapDrawable());
    popup.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss()
        {
            // Rotate back
            RotateAnimation animation = new RotateAnimation(-45.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(300);
            animation.setRepeatCount(0);
            animation.setFillAfter(true);
            animation.setFillBefore(true);
            animation.setFillEnabled(true);
            startAnimation(animation);
            animation.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation arg0)
                {
                    QuickActionButton.this.setClickable(false);
                }

                @Override
                public void onAnimationEnd(Animation arg0)
                {
                    QuickActionButton.this.setClickable(true);
                }

                @Override
                public void onAnimationRepeat(Animation arg0)
                {
                }
            });
        }
    });
    popup.setTouchable(false);
    popup.showAsDropDown(this, 0, -dpToPx(350));

    // Rotate the icon
    RotateAnimation animation = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(300);
    animation.setRepeatCount(0);
    animation.setFillAfter(true);
    animation.setFillBefore(true);
    animation.setFillEnabled(true);
    startAnimation(animation);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation arg0)
        {
            popup.setTouchable(false);
        }

        @Override
        public void onAnimationEnd(Animation arg0)
        {
            popup.setTouchable(true);
        }

        @Override
        public void onAnimationRepeat(Animation arg0)
        {
        }
    });

现在的问题是当我滚动视图并按下quick action button时,动画开始并在几毫秒后重置。

由于Popup

,这是正常的行为吗?

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决这个问题的肮脏方法:

handler.postDelayed(new Runnable() {
    @Override
    public void run()
    {
        if (!popup.isShowing())
        {
            handler.postDelayed(this, 100);
            return;
        }

        // Rotate the icon
        RotateAnimation animation = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(300);
        animation.setRepeatCount(0);
        animation.setFillAfter(true);
        animation.setFillBefore(true);
        animation.setFillEnabled(true);
        startAnimation(animation);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0)
            {
                popup.setTouchable(false);
            }

            @Override
            public void onAnimationEnd(Animation arg0)
            {
                popup.setTouchable(true);
            }

            @Override
            public void onAnimationRepeat(Animation arg0)
            {
            }
        });
    }
}, 100);

在弹出窗口完全显示之前,它只是一个延迟。