Saturday, February 25, 2017

android - View resize transition example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#a4ba93">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate Size"
        android:layout_margin="25dp"
        android:padding="25dp"
        android:layout_gravity="top|center_horizontal"
        android:gravity="center"
        />
    <View
        android:id="@+id/view_box"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:layout_gravity="center"
        android:layout_margin="15dp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.ChangeBounds;
import android.transition.TransitionManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private static final String TAG = MainActivity.class.getSimpleName();

    private CoordinatorLayout mCLayout;
    private Button mButton;
    private View mViewBox;

    private boolean isBig = false;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButton = (Button) findViewById(R.id.btn);
        mViewBox = (View) findViewById(R.id.view_box);

        mViewBox.getLayoutParams().width = 400;
        mViewBox.getLayoutParams().height = 200;

        // Set a click listener for button
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    ChangeBounds
                        This transition captures the layout bounds of target views before and after
                        the scene change and animates those changes during the transition.
                */

                // Initialize a new ChangeBounds transition instance
                ChangeBounds changeBounds = new ChangeBounds();

                // Set the transition start delay
                changeBounds.setStartDelay(300);

                // Set the transition interpolator
                changeBounds.setInterpolator(new AnticipateOvershootInterpolator());

                // Specify the transition duration
                changeBounds.setDuration(1000);

                // Begin the delayed transition
                TransitionManager.beginDelayedTransition(mCLayout,changeBounds);

                // Toggle the button size
                toggleSize(mViewBox);
            }
        });
    }

    // Custom method to toggle view size
    protected void toggleSize(View v){
        ViewGroup.LayoutParams params = v.getLayoutParams();
        if(isBig){
            params.width = 400;
            params.height = 200;
            isBig = false;

        }else {
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.MATCH_PARENT;
            isBig = true;
        }
        v.setLayoutParams(params);
    }
}