Thursday, August 11, 2016

Android different Interpolator 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="#f73f8c"
    >
    <Button
        android:id="@+id/btn_accelerate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accelerate"
        />
    <Button
        android:id="@+id/btn_bounce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bounce"
        app:layout_anchor="@id/btn_accelerate"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_decelerate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Decelerate"
        app:layout_anchor="@id/btn_bounce"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Linear"
        app:layout_anchor="@id/btn_decelerate"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_anticipate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Anticipate"
        app:layout_anchor="@id/btn_linear"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_cycle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cycle"
        app:layout_anchor="@id/btn_anticipate"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_overshoot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Overshoot"
        app:layout_anchor="@id/btn_cycle"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
    <Button
        android:id="@+id/btn_animate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate Me"
        android:layout_gravity="center_horizontal|top"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.animation.ObjectAnimator;
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.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;


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

    private CoordinatorLayout mCLayout;
    private Button mBtnAccelerate;
    private Button mBtnBounce;
    private Button mBtnDecelerate;
    private Button mBtnLinear;
    private Button mBtnAnticipate;
    private Button mBtnCycle;
    private Button mBtnOvershoot;
    private Button mBtnAnimate;


    @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);
        mBtnAccelerate = (Button) findViewById(R.id.btn_accelerate);
        mBtnBounce = (Button) findViewById(R.id.btn_bounce);
        mBtnDecelerate = (Button) findViewById(R.id.btn_decelerate);
        mBtnLinear = (Button) findViewById(R.id.btn_linear);
        mBtnAnticipate = (Button) findViewById(R.id.btn_anticipate);
        mBtnCycle = (Button) findViewById(R.id.btn_cycle);
        mBtnOvershoot = (Button) findViewById(R.id.btn_overshoot);
        mBtnAnimate = (Button) findViewById(R.id.btn_animate);

        // Initialize a new ObjectAnimator
        final ObjectAnimator moveWithInterpolator = ObjectAnimator.ofFloat(
                mBtnAnimate, // Target object
                "Y", // Property
                500 // ending value
        );

        // Set the animation duration in milliseconds
        // 1000 milliseconds = 1 second
        moveWithInterpolator.setDuration(3000);

        // Set a click listener for the accelerate button
        mBtnAccelerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    AccelerateInterpolator
                        An interpolator where the rate of change starts out slowly
                        and and then accelerates.
                */
                moveWithInterpolator.setInterpolator(new AccelerateInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the bounce button
        mBtnBounce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    BounceInterpolator
                        An interpolator where the change bounces at the end.
                */
                moveWithInterpolator.setInterpolator(new BounceInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the decelerate button
        mBtnDecelerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    DecelerateInterpolator
                        An interpolator where the rate of change starts out quickly and
                        and then decelerates.
                */
                moveWithInterpolator.setInterpolator(new DecelerateInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the linear button
        mBtnLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    LinearInterpolator
                        An interpolator where the rate of change is constant.
                */
                moveWithInterpolator.setInterpolator(new LinearInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the anticipate button
        mBtnAnticipate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    AnticipateInterpolator
                        An interpolator where the change starts backward then flings forward.
                */
                moveWithInterpolator.setInterpolator(new AnticipateInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the cycle button
        mBtnCycle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    CycleInterpolator
                        Repeats the animation for a specified number of cycles. The rate of
                        change follows a sinusoidal pattern.
                */
                moveWithInterpolator.setInterpolator(new CycleInterpolator(2.0f));

                // Start the animation
                moveWithInterpolator.start();
            }
        });

        // Set a click listener for the over shoot button
        mBtnOvershoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the animation interpolator
                /*
                    OvershootInterpolator
                        An interpolator where the change flings forward and
                        overshoots the last value then comes back.
                */
                moveWithInterpolator.setInterpolator(new OvershootInterpolator());

                // Start the animation
                moveWithInterpolator.start();
            }
        });
    }
}