Wednesday, February 1, 2017

android - Animate text color using ObjectAnimator

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="#f4f5f7"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate Color"
        android:layout_margin="25dp"
        android:layout_gravity="top|center_horizontal"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="45sp"
        android:gravity="center_horizontal"
        app:layout_anchor="@id/btn"
        android:layout_gravity="bottom|center_horizontal"
        app:layout_anchorGravity="bottom|center_horizontal"
        android:text="HELLO ANDROID"
        android:textStyle="bold"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


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

    private CoordinatorLayout mCLayout;
    private Button mButton;
    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);
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);

        // Initialize a new click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    ObjectAnimator
                        This subclass of ValueAnimator provides support for animating properties on
                        target objects. The constructors of this class take parameters to define the
                        target object that will be animated as well as the name of the property that
                        will be animated. Appropriate set/get functions are then determined
                        internally and the animation will call these functions as necessary
                        to animate the property.
                */
                /*
                    ArgbEvaluator
                        This evaluator can be used to perform type interpolation between
                        integer values that represent ARGB colors.
                */
                ObjectAnimator.ofObject(
                        mTextView, // Object to animating
                        "textColor", // Property to animate
                        new ArgbEvaluator(), // Interpolation function
                        Color.BLUE, // Start color
                        Color.RED // End color
                ).setDuration(1200) // Duration in milliseconds
                .start(); // Finally, start the anmation
            }
        });
    }
}