How does one Animate Layout properties of ViewGroups?

后端 未结 3 1159
别那么骄傲
别那么骄傲 2020-12-15 01:07

I have the following layout:




        
3条回答
  •  萌比男神i
    2020-12-15 01:53

    Since noone helped you yet and my first answer was such a mess I'll try to give you the right answer this time ;-)

    Actually I like the idea and I think this is a great visual effect which might be useful for a bunch of people. I would implement an overflow of the right view (I think the shrink looks strange since the text is expanding to the bottom).

    But anyway, here's the code which works perfectly fine (you can even toggle while it's animating).

    Quick explanation:
    You call toggle with a boolean for your direction and this will start a handler animation call loop. This will increase or decrease the weights of both views based on the direction and the past time (for a smooth calculation and animation). The animation call loop will invoke itself as long it hasn't reached the start or end position.

    The layout:

    
    
        
        
    
    

    The activity:

    public class TestActivity extends Activity {
    
        private static final int ANIMATION_DURATION = 1000;
    
        private View mSlidingLayout;
        private View mLeftView;
        private View mRightView;
    
        private boolean mAnimating = false;
        private boolean mLeftExpand = true;
        private float mLeftStartWeight;
        private float mLayoutWeightSum;
        private Handler mAnimationHandler = new Handler();
        private long mAnimationTime;
    
        private Runnable mAnimationStep = new Runnable() {
            @Override
            public void run() {
                long currentTime = System.currentTimeMillis();
                float animationStep = (currentTime - mAnimationTime) * 1f / ANIMATION_DURATION;
                float weightOffset = animationStep * (mLayoutWeightSum - mLeftStartWeight);
    
                LinearLayout.LayoutParams leftParams = (LinearLayout.LayoutParams)
                        mLeftView.getLayoutParams();
                LinearLayout.LayoutParams rightParams = (LinearLayout.LayoutParams)
                        mRightView.getLayoutParams();
    
                leftParams.weight += mLeftExpand ? weightOffset : -weightOffset;
                rightParams.weight += mLeftExpand ? -weightOffset : weightOffset;
    
                if (leftParams.weight >= mLayoutWeightSum) {
                    mAnimating = false;
                    leftParams.weight = mLayoutWeightSum;
                    rightParams.weight = 0;
                } else if (leftParams.weight <= mLeftStartWeight) {
                    mAnimating = false;
                    leftParams.weight = mLeftStartWeight;
                    rightParams.weight = mLayoutWeightSum - mLeftStartWeight;
                }
    
                mSlidingLayout.requestLayout();
    
                mAnimationTime = currentTime;
    
                if (mAnimating) {
                    mAnimationHandler.postDelayed(mAnimationStep, 30);
                }
            }
        };
    
        private void toggleExpand(boolean expand) {
            mLeftExpand = expand;
    
            if (!mAnimating) {
                mAnimating = true;
                mAnimationTime = System.currentTimeMillis();
                mAnimationHandler.postDelayed(mAnimationStep, 30);
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.slide_test);
    
            mLeftView = findViewById(R.id.left);
            mRightView = findViewById(R.id.right);
            mSlidingLayout = findViewById(R.id.slide_layout);
    
            mLeftStartWeight = ((LinearLayout.LayoutParams)
                    mLeftView.getLayoutParams()).weight;
            mLayoutWeightSum = ((LinearLayout) mSlidingLayout).getWeightSum();
        }
    }
    

提交回复
热议问题