Anyway to programmatically animate layout weight property of linear layout

前端 未结 5 1771
暗喜
暗喜 2021-02-18 15:12

I have two views in a linear layout, I programmatically change their layout_weight property. Is there a way I could animate this change in weight so when the weight is changed v

5条回答
  •  渐次进展
    2021-02-18 15:52

    Note: I am not sure that this is the best way, but I tried it and it's working fine

    Simply using ValueAnimator

    ValueAnimator m1 = ValueAnimator.ofFloat(0.2f, 0.5f); //fromWeight, toWeight
    m1.setDuration(400);
    m1.setStartDelay(100); //Optional Delay
    m1.setInterpolator(new LinearInterpolator());
    m1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
             @Override
             public void onAnimationUpdate(ValueAnimator animation) {
                 ((LinearLayout.LayoutParams) viewToAnimate.getLayoutParams()).weight = (float) animation.getAnimatedValue();
                 viewToAnimate.requestLayout();
             }
    
    });
    m1.start();
    

    More About ValueAnimator

提交回复
热议问题