How to create gradient background animation in android

In this android programming source code example, we are going to create gradient background animation in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create gradient background animation in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".animation.Animation18"
    android:id="@+id/linearLayout"
    android:background="@drawable/gradient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animated Background"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

MainActivity.java

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;


public class Animation18 extends AppCompatActivity {

    AnimationDrawable animationDrawable;
    LinearLayout linearLayout;

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

        linearLayout = findViewById(R.id.linearLayout);
        animationDrawable = (AnimationDrawable) linearLayout.getBackground();
        animationDrawable.setEnterFadeDuration(3000);
        animationDrawable.setExitFadeDuration(2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (animationDrawable  != null && !animationDrawable.isRunning()) {
            animationDrawable.start();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
        }
    }
}

drawable/gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:duration="5000"
        android:drawable="@drawable/gradient1"/>

    <item android:duration="5000"
        android:drawable="@drawable/gradient2"/>

    <item android:duration="5000"
        android:drawable="@drawable/gradient3"/>

</animation-list>

drawable/gradient1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:type="linear"
        android:angle="315"
        android:startColor="@color/colorGradientStart"
        android:centerColor="@color/colorGradientCenter"
        android:endColor="@color/colorGradientEnd"/>
</shape>

drawable/gradient2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:type="linear"
        android:angle="315"
        android:startColor="@color/colorGradientCenter"
        android:centerColor="@color/colorGradientEnd"
        android:endColor="@color/colorGradientStart"/>
</shape>

drawable/gradient3.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:type="linear"
        android:angle="315"
        android:startColor="@color/colorGradientEnd"
        android:centerColor="@color/colorGradientStart"
        android:endColor="@color/colorGradientCenter"/>
</shape>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment