2

Is it possible to animate a layout_weight change of a View?

I have tried:

ObjectAnimator anim = ObjectAnimator.ofFloat(headerRoot, "layout_weight", ws, 1f); anim.setDuration(1500); anim.addUpdateListener(this); anim.start();

This has no effect on my layout. Is the objectAnimator able to manipulate the layout_weight property of a view?

Adrian
  • 407
  • 6
  • 20
  • possible duplicate of [Change the weight of layout with an animation](http://stackoverflow.com/questions/18024591/change-the-weight-of-layout-with-an-animation) – Piotr Golinski Apr 17 '15 at 13:28
  • instead of "layout_weight" you can use whatever e. g. "blaBlaBla", then create a public void setBlaBlaBla(float f) method in a class that you pass a first parameter to ObjectAnimator.ofFloat() add some Log.d inside that method and everything will be clear on how to proceed – pskink Apr 17 '15 at 13:40
  • and if you don't want to use an ObjectAnimator you can always use a ValueAnimator – pskink Apr 17 '15 at 16:32
  • see my answer to similar question here http://stackoverflow.com/a/34090038/2712372 – kostya17 Dec 04 '15 at 14:13

2 Answers2

1

Just use a class provided here: https://stackoverflow.com/a/20334557/1763138

private class ExpandAnimation extends Animation {

    private final float mStartWeight;
    private final float mDeltaWeight;

    public ExpandAnimation(float startWeight, float endWeight) {
        mStartWeight = startWeight;
        mDeltaWeight = endWeight - startWeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContent.getLayoutParams();
        lp.weight = (mStartWeight + (mDeltaWeight * interpolatedTime));
        mContent.setLayoutParams(lp);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

and change mContent to your headerRoot

Community
  • 1
  • 1
rasmeta
  • 465
  • 2
  • 12
0

This has no effect on my layout. Is the objectAnimator able to manipulate the layout_weight property of a view?

No, it is not. ObjectAnimator uses the setter method of an object to update its value. A LayoutParam has not setWeight method, it only has the weight property you can change.

It's all explained here: http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator.

ar34z
  • 2,609
  • 2
  • 24
  • 37