Android TranslateAnimation resets after animation

后端 未结 6 1360
灰色年华
灰色年华 2020-12-02 10:13

I\'m creating something like a SlideDrawer but with most customization, basically the thing is working but the animation is flickering at the end.

To further explain

6条回答
  •  旧时难觅i
    2020-12-02 10:57

    Soham's answer above works for me, although it's worth pointing out (since it wasn't immediately obvious to me when first reading this thread) that you can still get very nearly the same behavior as an animation listener by setting a separate listener on the view to be run at the end of your View's onAnimationStart() and onAnimationEnd().

    For instance, if your code needs to disable a button for the duration of an animation:

    Animation a = getAnimation(/* your code */);
    a.setDuration(1000);
    a.setAnimationListener(new AnimationListener() {
       @Override
       public void onAnimationStart(Animation arg0) {
         myButton.setEnabled(false);
       }
    
       @Override
       public void onAnimationEnd(Animation arg0) {
         myButton.setEnabled(true);
       }
    });
    someView.startAnimation(a);
    

    Currently, someView doesn't know about myButton, and I'd like to keep it that way. You can just create some listener on your custom view class that gets called in the same fashion:

    public final class SomeView extends View {
        // other code
    
        public interface RealAnimationListener {
          public void onAnimationStart();
          public void onAnimationEnd();
        }
    
        private RealAnimationListener mRealAnimationListener;
    
        public void setRealAnimationListener(final RealAnimationListener listener) {
          mRealAnimationListener = listener;
        }
    
        @Override
        protected void onAnimationStart() {
          super.onAnimationStart();
          if (mRealAnimationListener != null) {
             mRealAnimationListener.onAnimationStart();
          }
        }
    
        @Override
        protected void onAnimationEnd() {
          super.onAnimationEnd();
          if (mRealAnimationListener != null) {
             mRealAnimationListener.onAnimationEnd();
          }
        }
    }
    

    And then back in your other code (probably an Activity):

    Animation a = getAnimation(/* your code */);
    a.setDuration(1000);
    someView.setRealAnimationListener(new RealAnimationListener() {
       @Override
       public void onAnimationStart() {
         myButton.setEnabled(false);
       }
    
       @Override
       public void onAnimationEnd() {
         myButton.setEnabled(true);
       }
    });
    someView.startAnimation(a);
    

    This way you keep your components separated cleanly while still getting an AnimationListener that works.

提交回复
热议问题