Android: Animation Position Resets After Complete

后端 未结 9 1467
南笙
南笙 2020-11-27 13:30

I\'m using an xml defined animation to slide a view off the screen. The problem is, as soon as the animation completes it resets to its original position. I need to know how

相关标签:
9条回答
  • 2020-11-27 13:42

    fillAfter = true will do the job to transform view to new animation positon.

    rotateAnimation.fillAfter = true;
    

    If anybody interested to move arrow_up & arrow_down animations for a recycle view expands/collapse behavior.

    1. Initial state: Collapse
    2. call the method with a child visibility flag(View.VISIBLE/View.GONE).

    Below is code belongs to animations.

    fun setArrowImage(imageView: ImageView, childVisibility: Int) {
    
    val angleRange = if (childVisibility == View.VISIBLE) Pair(0F, 180F) else 
    Pair(100f, 0F)
    
    val rotateAnimation = RotateAnimation(
        angleRange.first, angleRange.second,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
    )
    rotateAnimation.duration = 300
    rotateAnimation.fillAfter = true;
    
    imageView.startAnimation(rotateAnimation)
    

    }

    0 讨论(0)
  • 2020-11-27 13:44

    Finally got a way to work around,the right way to do this is setFillAfter(true),

    if you want to define your animation in xml then you should do some thing like this

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:interpolator="@android:anim/decelerate_interpolator"
         android:fillAfter="true">
    
        <translate 
            android:fromXDelta="0%"
            android:toXDelta="-100%"
            android:duration="1000"/>
    
    </set>
    

    you can see that i have defined filterAfter="true" in the set tag,if you try to define it in translate tag it won't work,might be a bug in the framework!!

    and then in the Code

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
    someView.startAnimation(anim);
    

    OR

    TranslateAnimation animation = new TranslateAnimation(-90, 150, 0, 0);
    
    animation.setFillAfter(true);
    
    animation.setDuration(1800);
    
    someView.startAnimation(animation);
    

    then it will surely work!!

    Now this is a bit tricky it seems like the view is actually move to the new position but actually the pixels of the view are moved,i.e your view is actually at its initial position but not visible,you can test it if have you some button or clickable view in your view(in my case in layout),to fix that you have to manually move your view/layout to the new position

    public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
    
    new TranslateAnimation(-90, 150, 0, 0);
    

    now as we can see that our animation will starts from -90 x-axis to 150 x-axis

    so what we do is set

    someView.setAnimationListener(this);
    

    and in

    public void onAnimationEnd(Animation animation)
    {
       someView.layout(150, 0, someView.getWidth() + 150, someView.getHeight());
    }
    

    now let me explain public void layout (int left, int top, int right, int botton)

    it moves your layout to new position first argument define the left,which we is 150,because translate animation has animated our view to 150 x-axis, top is 0 because we haven't animated y-axis,now in right we have done someView.getWidth() + 150 basically we get the width of our view and added 150 because we our left is now move to 150 x-axis to make the view width to its originall one, and bottom is equals to the height of our view.

    I hope you people now understood the concept of translating, and still you have any questions you can ask right away in comment section,i feel pleasure to help :)

    EDIT Don't use layout() method as it can be called by the framework when ever view is invalidated and your changes won't presist, use LayoutParams to set your layout parameters according to your requirement

    0 讨论(0)
  • 2020-11-27 13:44

    This problem has bothered for more than a week. And Muhammad's answer pointed me a strightforward way to solve it.

    I used sever layout to implement side menus with animation. "view.layout will not be persistent. You should use LayoutParams, which will be persistent."

    Here's my solution: (Use what kind of LayoutParameter is depended on your own parent's layout):

    @Override
    public void onAnimationEnd(Animation animation) {
    FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(screenWidth, screenHeight);
    layoutParams.setMargins((int) -(screenWidth * RATE), 0,
                (int) (screenWidth - screenWidth * RATE), screenHeight);
        for(View v:views) {
    
            v.setLayoutParams(layoutParams);
            //v.layout((int) -(screenWidth * RATE), 0, (int) (screenWidth - screenWidth * RATE), screenHeight);
            v.clearAnimation();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:48

    I might be a bit late in replying, but I have the solution for this:

    Just add android:fillAfter="true" in your xml

    0 讨论(0)
  • 2020-11-27 13:53

    You need to add this command:

    final Animation animSlideLeft = new TranslateAnimation(0, -80, 0,-350, 0, 0, 0, 0);
    animSlideLeft.setFillAfter(true);
    
    0 讨论(0)
  • 2020-11-27 13:53

    Use helper methods below to easily animate your view. Then you can animate and reset after completion like this:

    // Animate the view:
    
    fly(
      view1,
      (float) 0,
      (float) 0,
      (float) 0,
      (float) 1000,
      250,
      null,
      null,
      () -> {
    
        // Return the view to initial position on animation end:  
    
        fly(
          view1,
          (float) 0,
          (float) 0,
          (float) 0,
          (float) 0,
          0);
    
        return null;
      });
    

    Helper methods:

    /**
     * Translation of a view.
     */
    public static void fly(
      View view,
      float fromXDelta,
      float toXDelta,
      float fromYDelta,
      float toYDelta,
      int duration) {
    
      fly(
        view,
        fromXDelta,
        toXDelta,
        fromYDelta,
        toYDelta,
        duration,
        null,
        null,
        null);
    }
    
    /**
     * Translation of a view with handlers.
     *
     * @param view       A view to animate.
     * @param fromXDelta Amount to shift by X axis for start position.
     * @param toXDelta   Amount to shift by X axis for end position.
     * @param fromYDelta Amount to shift by Y axis for start position.
     * @param toYDelta   Amount to shift by Y axis for end position.
     * @param duration   Animation duration.
     * @param start      On animation start. Otherwise NULL.
     * @param repeat     On animation repeat. Otherwise NULL.
     * @param end        On animation end. Otherwise NULL.
     */
    public static void fly(
      View view,
      float fromXDelta,
      float toXDelta,
      float fromYDelta,
      float toYDelta,
      int duration,
      Callable start,
      Callable repeat,
      Callable end) {
    
      Animation animation;
    
      animation =
        new TranslateAnimation(
          fromXDelta,
          toXDelta,
          fromYDelta,
          toYDelta);
    
      animation.setDuration(
        duration);
    
      animation.setInterpolator(
        new DecelerateInterpolator());
    
      animation.setFillAfter(true);
    
      view.startAnimation(
        animation);
    
      animation.setAnimationListener(new AnimationListener() {
    
        @Override
        public void onAnimationStart(Animation animation) {
    
          if (start != null) {
            try {
    
              start.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
    
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
    
          if (end != null) {
            try {
    
              end.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
    
        @Override
        public void onAnimationRepeat(
          Animation animation) {
    
          if (repeat != null) {
            try {
    
              repeat.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }  
        }
      });
    }
    
    0 讨论(0)
提交回复
热议问题