Monday, February 20, 2017

android - Circular reveal animation 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="#e5e9ea">
    <Button
        android:id="@+id/btn_do"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Circular Reveal Animation"
        android:layout_margin="25dp"
        android:layout_gravity="top|center_horizontal"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:text="Sample TextView"
        app:layout_anchor="@id/btn_do"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        android:layout_margin="10dp"
        android:background="#4277d7"
        android:gravity="center"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
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.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;


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

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

    private CoordinatorLayout mCLayout;
    private Button mButtonDo;
    private TextView mTextView;


    @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);
        mButtonDo = (Button) findViewById(R.id.btn_do);
        mTextView = (TextView) findViewById(R.id.tv);

        // Set a click listener for button widget
        mButtonDo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do something
                // Start the circular reveal animation for text view
                startCircularRevealAnimation(mTextView);
            }
        });
    }

    // Custom method to create a circular reveal animation for specified view
    private void startCircularRevealAnimation(final View v) {
        // Get the specified view center x
        int centerX = (v.getLeft() + v.getRight())/2;
        // Get the specified view center y
        int centerY = (v.getTop() + v.getBottom())/2;
        // Get the specified view radius
        float radius = Math.max(v.getWidth(),v.getHeight()) * 2.0f;

        // If the view is invisible state
        if(v.getVisibility() == View.INVISIBLE){
            v.setVisibility(View.VISIBLE);

            // Call requires API level 21
            ViewAnimationUtils.createCircularReveal(
                    v, // View
                    centerX, // centerX
                    centerY, // centerY
                    0, // startRadius
                    radius // endRadius
            ).start();
        }else { // If the view is visible state
            // Call requires API level 21
            // Initialize a new circular reveal animation instance
            Animator reveal = ViewAnimationUtils.createCircularReveal(
                    v, // View
                    centerX, // centerX
                    centerY, // centerY
                    radius, // startRadius
                    0 // endRadius
            );
            // Add a listener
            reveal.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    v.setVisibility(View.INVISIBLE);
                }
            });
            // Start the circular reveal animation
            reveal.start();
        }
    }
}