Saturday, February 25, 2017

android - Arc motion 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="#95aafd">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUTTON"
        />
</android.support.design.widget.CoordinatorLayout>
res/transition/arc_motion_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<changeBounds
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/decelerate_cubic"
    >
    <!--
        ArcMotion
            A PathMotion that generates a curved path along an arc on
            an imaginary circle containing the two points.

            android:maximumAngle
                The maximum arc angle in degrees between the start and end points.

            android:minimumHorizontalAngle
                The minimum arc angle in degrees between the start and end points
                when they are close to horizontal.

            android:minimumVerticalAngle
                The minimum arc angle in degrees between the start and end points
                 when they are close to vertical.
    -->
    <arcMotion
        android:maximumAngle="45"
        android:minimumHorizontalAngle="90"
        android:minimumVerticalAngle="0"
        />
</changeBounds>
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.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.Gravity;
import android.view.View;
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 boolean isTop = true;


    @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);

        // Set a click listener for layout
        mCLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the transition id
                int transitionId = R.transition.arc_motion_transition;

                // Get the transition inflater
                TransitionInflater inflater = TransitionInflater.from(mContext);

                // Inflate the specified transition
                Transition arcMotionTransition = inflater.inflateTransition(transitionId);

                // Aet the transition duration
                arcMotionTransition.setDuration(1000);

                // Specify the target for the transition
                arcMotionTransition.addTarget(mButton);

                // Add a listener for the transition
                arcMotionTransition.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {
                        // At the start of transition visible the target
                        mButton.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onTransitionEnd(Transition transition) {
                        // At the end of transition hide the target
                        if(!isTop){
                            TransitionManager.beginDelayedTransition(mCLayout);
                            mButton.setVisibility(View.INVISIBLE);
                        }
                    }

                    @Override
                    public void onTransitionCancel(Transition transition) {
                    }

                    @Override
                    public void onTransitionPause(Transition transition) {
                    }

                    @Override
                    public void onTransitionResume(Transition transition) {
                    }
                });

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

                // Toggle the button position
                togglePosition();
            }
        });
    }

    protected void togglePosition(){
        // Change the button widget location to animate it
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)mButton.getLayoutParams();

        if(isTop){
            // Put the button at the layout bottom horizontal center
            params.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
            isTop = false;
        }else {
            // Put the button at the layout top left
            params.gravity = Gravity.TOP|Gravity.LEFT;
            isTop = true;
        }
        mButton.setLayoutParams(params);
    }
}