Anyway to programmatically animate layout weight property of linear layout

前端 未结 5 1753
暗喜
暗喜 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:47

    Another way is to use old Animation class, as described in https://stackoverflow.com/a/20334557/2914140. In this case you can simultaneously change weights of several Views.

    private static class ExpandAnimation extends Animation {
        private final View[] views;
        private final float startWeight;
        private final float deltaWeight;
    
        ExpandAnimation(View[] views, float startWeight, float endWeight) {
            this.views = views;
            this.startWeight = startWeight;
            this.deltaWeight = endWeight - startWeight;
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            float weight = startWeight + (deltaWeight * interpolatedTime);
            for (View view : views) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
                lp.weight = weight;
                view.setLayoutParams(lp);
            }
            views[0].getParent().requestLayout();
        }
    
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }
    

提交回复
热议问题