Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Last active February 19, 2023 17:43
Show Gist options
  • Save magdamiu/77389efb66ae9e693dcf1d5680fdf531 to your computer and use it in GitHub Desktop.
Save magdamiu/77389efb66ae9e693dcf1d5680fdf531 to your computer and use it in GitHub Desktop.
Gradient with Animation
// gradients
// gradient_1.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:endColor="#1a2980"
android:startColor="#26d0ce" />
</shape>
// gradient_2.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#614385"
android:startColor="#516395" />
</shape>
// gradient_3.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="135"
android:endColor="#1d2b64"
android:startColor="#f8cdda" />
</shape>
// gradient_4.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#ff512f"
android:startColor="#dd2476" />
</shape>
// gradient_5.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="135"
android:endColor="#34e89e"
android:startColor="#0f3443" />
</shape>
// animation.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/gradient_1"
android:duration="5000" />
<item
android:drawable="@drawable/gradient_2"
android:duration="5000" />
<item
android:drawable="@drawable/gradient_3"
android:duration="5000" />
<item
android:drawable="@drawable/gradient_4"
android:duration="5000" />
<item
android:drawable="@drawable/gradient_5"
android:duration="5000" />
</animation-list>
// apply animation as a background
android:background="@drawable/animation"
// add an id to the element where you added the background
// in this example the element is a LinearLayout
android:id="@+id/layoutMain"
// java call
private void setAnimation() {
LinearLayout linearLayout = findViewById(R.id.layoutMain);
AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground();
animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment