How to create Animated Background Gradient ?

How to create Animated Background Gradient ?








OutPut :



activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:id="@+id/linearlayout1"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@drawable/gradient_animation"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Background Gradients!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:gravity="center"
        />

</LinearLayout>



Then After Creating this activity_main.xml file you have to create Four Different Gradients designs of differrent colors.

GO TO res > drawable > and create new drawable resource file.


gradient_one.xml

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
    <gradient
        android:angle = "135"
        android:endColor = "#FFC400"
        android:startColor = "#FF6F00" />
</shape>




gradient_two.xml

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
    <gradient
        android:angle = "0"
        android:endColor = "#00E676"
        android:startColor = "#1B5E20"/>
</shape>




gradient_three.xml


<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
    <gradient
        android:angle = "45"
        android:endColor = "#FF1744"
        android:startColor = "#B71C1C" />
</shape>



gradient_four.xml

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
    <gradient
        android:angle = "90"
        android:endColor = "#D500F9"
        android:startColor = "#4A148C" />
</shape>




After creating these four gradients xml files then you have to create one more drawable.xml file

gradient_animation.xml

<?xml version = "1.0" encoding = "utf-8"?>
<animation-list xmlns:android = "http://schemas.android.com/apk/res/android">
    <item
        android:drawable = "@drawable/gradients"
        android:duration = "3000" />
    <item
        android:drawable = "@drawable/gradient_two"
        android:duration = "3000" />
    <item
        android:drawable = "@drawable/gradient_three"
        android:duration = "3000" />
    <item
        android:drawable = "@drawable/gradient_four"
        android:duration = "3000" />
</animation-list>



At Last let's work on MainActivity.java

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private LinearLayout mLinearLayout;
    private AnimationDrawable animationDrawable;

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

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



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

    }

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

    }

0 Comments