0

I've implemented a small animation on a button placed over a list view. Basically it's like a quick action button to add/refresh the list.

What I want to do is to animate the button once the user has clicked on it and display a popup. Once the popup has been dismissed, the button should go back to its initial state.

I've made the followning code:

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

The problem now is that when I scroll the view and press the quick action button, the animation starts and is reseted after a few miliseconds.

Is that a normal behaviour because of the Popup ?

ligi
  • 39,001
  • 44
  • 144
  • 244
Manitoba
  • 8,522
  • 11
  • 60
  • 122

1 Answers1

0

Finally, I've found a dirty way to solve this issue:

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

It's just a delayed until the popup has been completely shown.

Manitoba
  • 8,522
  • 11
  • 60
  • 122