

package com.android.example.simpletransition;

import android.support.transition.ChangeBounds;
import android.support.transition.Transition;
import android.support.transition.TransitionManager;
import android.support.transition.TransitionSet;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import static android.widget.RelativeLayout.ABOVE;
import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
import static android.widget.RelativeLayout.ALIGN_PARENT_LEFT;
import static android.widget.RelativeLayout.ALIGN_PARENT_RIGHT;
import static android.widget.RelativeLayout.BELOW;
import static android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends AppCompatActivity {

    CheckBox mStaggerCB;
    Button mFirstButton, mSecondButton, mThirdButton, mFourthButton;
    ViewGroup mSceneRoot;
    TransitionSet mStaggeredTransition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStaggerCB = (CheckBox) findViewById(R.id.staggerCB);

        mFirstButton = (Button) findViewById(R.id.firstButton);
        mSecondButton = (Button) findViewById(R.id.secondButton);
        mThirdButton = (Button) findViewById(R.id.thirdButton);
        mFourthButton = (Button) findViewById(R.id.fourthButton);

        mSceneRoot = (ViewGroup) findViewById(R.id.activity_main);

        // Create custom transition that 'staggers' the animations by offsetting
        // the individual start times
        mStaggeredTransition = new TransitionSet();
        Transition first = new ChangeBounds();
        Transition second = new ChangeBounds();
        Transition third = new ChangeBounds();
        Transition fourth = new ChangeBounds();

        first.addTarget(mFirstButton);
        second.setStartDelay(50).addTarget(mSecondButton);
        third.setStartDelay(100).addTarget(mThirdButton);
        fourth.setStartDelay(150).addTarget(mFourthButton);

        mStaggeredTransition.addTransition(first).addTransition(second).addTransition(third).
                addTransition(fourth);
    }

    private void alignButtons(boolean left, boolean top) {
        LayoutParams params;

        // Trigger a transition to run after the next layout pass
        if (mStaggerCB.isChecked()) {
            TransitionManager.beginDelayedTransition(mSceneRoot, mStaggeredTransition);
        } else {
            TransitionManager.beginDelayedTransition(mSceneRoot);
        }

        // Change layout parameters of the button stack
        int oldAlignmentLR = left ? ALIGN_PARENT_RIGHT : ALIGN_PARENT_LEFT;
        int newAlignmentLR = left ? ALIGN_PARENT_LEFT : ALIGN_PARENT_RIGHT;
        int oldAlignmentTB = top ? ABOVE : BELOW;
        int newAlignmentTB = top ? BELOW : ABOVE;

        params = (LayoutParams) mFirstButton.getLayoutParams();
        params.addRule(top ? ALIGN_PARENT_BOTTOM : BELOW, 0);
        params.addRule(oldAlignmentLR, 0);
        params.addRule(top ? BELOW : ALIGN_PARENT_BOTTOM, top ? R.id.staggerCB : 1);
        params.addRule(newAlignmentLR);
        mFirstButton.setLayoutParams(params);

        params = (LayoutParams) mSecondButton.getLayoutParams();
        params.addRule(oldAlignmentLR, 0);
        params.addRule(oldAlignmentTB, 0);
        params.addRule(newAlignmentLR);
        params.addRule(newAlignmentTB, R.id.firstButton);
        mSecondButton.setLayoutParams(params);

        params = (LayoutParams) mThirdButton.getLayoutParams();
        params.addRule(oldAlignmentLR, 0);
        params.addRule(oldAlignmentTB, 0);
        params.addRule(newAlignmentLR);
        params.addRule(newAlignmentTB, R.id.secondButton);
        mThirdButton.setLayoutParams(params);

        params = (LayoutParams) mFourthButton.getLayoutParams();
        params.addRule(oldAlignmentLR, 0);
        params.addRule(oldAlignmentTB, 0);
        params.addRule(newAlignmentLR);
        params.addRule(newAlignmentTB, R.id.thirdButton);
        mFourthButton.setLayoutParams(params);
    }

    public void onClick(View view) {
        switch (((Button) view).getText().toString()) {
            case "Top Left":
                alignButtons(true, true);
                break;

            case "Top Right":
                alignButtons(false, true);
                break;

            case "Bottom Left":
                alignButtons(true, false);
                break;

            case "Bottom Right":
                alignButtons(false, false);
                break;
        }
    }
}
