Android - Change left margin using animation

后端 未结 4 962
别那么骄傲
别那么骄傲 2020-12-07 18:25

I am changing the left margin of an image view in the following manner :

ViewGroup.MarginLayoutParams layoutParams =         


        
相关标签:
4条回答
  • 2020-12-07 18:56

    You can use the following

    image.animate().setDuration(durationIn).translationXBy(offsetFloat).start();
    

    You can also add .setInterpolator(new BounceInterpolator()) to change the look of the animation.

    0 讨论(0)
  • 2020-12-07 18:58

    I came by this question, but I couldn't use it because I want to animate the margin from a negative value to 0, so I used valueAnimater base on user1991679 answer:

    final View animatedView = view.findViewById(R.id.animatedView);
    final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) animatedView.getLayoutParams();
    ValueAnimator animator = ValueAnimator.ofInt(params.bottomMargin, 0);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator)
        {
            params.bottomMargin = (Integer) valueAnimator.getAnimatedValue();
            animatedView.requestLayout();
        }
    });
    animator.setDuration(300);
    animator.start();
    

    You must change LinearLayout.LayoutParams according to animatedView container. Also you can use nineoldandroids for older version that don't have ValueAnimator.

    0 讨论(0)
  • 2020-12-07 19:11

    Use Animation class, not ObjectAnimator.

    final int newLeftMargin = <some value>;
    Animation a = new Animation() {
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            LayoutParams params = yourView.getLayoutParams();
            params.leftMargin = (int)(newLeftMargin * interpolatedTime);
            yourView.setLayoutParams(params);
        }
    };
    a.setDuration(500); // in ms
    yourView.startAnimation(a);
    

    Please note that you should use correct LayoutParams class i.e. if your view is the child of LinearLayout then params should be LinearLayout.LayoutParams

    0 讨论(0)
  • 2020-12-07 19:20

    The answer from user1991679 is great, but if you need to interpolate a margin from any other value but 0, you need to use it in your calculations:

    ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams();
    final int bottomMarginStart = params.bottomMargin; // your start value
    final int bottomMarginEnd = <your value>; // where to animate to
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams();
            // interpolate the proper value
            params.bottomMargin = bottomMarginStart + (int) ((bottomMarginEnd - bottomMarginStart) * interpolatedTime);
            mBottomLayout.setLayoutParams(params);
        }
    };
    a.setDuration(300);
    mBottomLayout.startAnimation(a);
    

    In my case I needed to animate an "enter the screen" animation, coming from "-48dp" to 0. Without the start value, the animation is always 0, thus jumping, not animating the view. The solution was to interpolate the offset and add it to the original value.

    0 讨论(0)
提交回复
热议问题