elevenetc / BadgeView

Badge view with animated effect which shows a bitmap or a text
448 stars 92 forks source link

The BadgeView's dimensions/animation goes weird when you leave the activity and the animation is running (I have a video demonstration) #3

Open Meeks91 opened 8 years ago

Meeks91 commented 8 years ago

The BadgeView's dimensions/animation goes weird, in that view's size changes, and the animation changes with the size.

This occurs when you when you leave the activity and the animation is running, for example, if you exit the activity to look at the multi-tasking menu in Android (onPause and onStop are called), and come back into the app this problem will occur, this doesn't happen every time, but it does happen.

This problem affects the sample app, and my own implementation.

I have uploaded a video to demonstrate the problem, I will take it down/make it private if you ask me to.

One solution is to allow the animation to finish and then restart it when entering the app.

I haven't found a way to successfully stop it, but if I leave the app when the app has finished and restart it, it works (I can provide this code if you need it).

Link to the video: https://www.youtube.com/watch?v=MFirApwfcpQ&feature=youtu.be Error occurs at roughly 22 seconds+

Any ideas on a good fix ?

Thanks in advance.

Meeks91 commented 8 years ago

Okaaaaaay, so I fixed it (disclaimer: only tried it on Lollipop, and it hasn't crashed so far), I don't know if you can think of a better solution but this is what I did.

I copied over and made my own editable AbstractBadgeView and BadgeView classes.

Then, in the AbstractBadgeView class I made a member boolean variable called: "inActivity", which is set to, true, when the user is in the activity.

Then in these methods in the AbstractBadgeView class:

 private void handleAnimation(AbstractBadgeView2.ValueAnimation a)

private void handleAnimationListener() 

  private void animateShiftAndSetValue(final IValue<?> newValue, float newHeight, long duration)

private void changeBackgroundSize(IValue<?> newValue, boolean animate, long duration) 

 protected void setValue(@NonNull IValue newValue, boolean animate, long duration) 

I wrapped the code in each behaviour in the inActivity boolean and only allowed them to run inActivity is set to true.

Then in my android activity where I use the BadgeView, when onPause() is called, I set inActivity false, stopping those methods from running. Then when onStart() is called I set it back to true, and I call this method, where I initialise the animation like so:

 badgeView.postDelayed(new Runnable() {
                @Override
                public void run() {

                     badgeAnimator
                            .add("I am a String", duration)
                                  .add(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo), duration)

                            .play(MyImplentingActivity.this);
               }
            }, 1000);  

I haven't tested it too much, but so far it hasn't failed me once.

Let me know if you come up with your own solution or you want my code.