Skip to content

Instantly share code, notes, and snippets.

@deepak786
Last active April 27, 2022 14:30
Show Gist options
  • Save deepak786/12eee8412fa7914a16774ce546e1a089 to your computer and use it in GitHub Desktop.
Save deepak786/12eee8412fa7914a16774ce546e1a089 to your computer and use it in GitHub Desktop.
Instagram Like Gradient Color Transition in Android
/******This Gist explains how to create instagram like Gradient color transition in android.******/
1. Create some gradient color drawables inside drawable Folder.
a) color1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#c44e4e"
android:endColor="#dcb9b9"
android:angle="0"/>
</shape>
b) color2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#680b0b"
android:endColor="#c6b147"
android:angle="45"/>
</shape>
c) color3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#57caa8"
android:endColor="#44c74b"
android:angle="90"/>
</shape>
d) color4.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#7141e2"
android:endColor="#d46cb3"
android:angle="135"/>
</shape>
2. Create animation list using the above created gradient colors, animation_list.xml, inside drawable folder
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/color1"
android:duration="10000" />
<item
android:drawable="@drawable/color2"
android:duration="10000" />
<item
android:drawable="@drawable/color3"
android:duration="10000" />
<item
android:drawable="@drawable/color4"
android:duration="10000" />
</animation-list>
3. Apply the animation_list created above as a background to the top view of your activity layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/animation_list"
android:id="@+id/container">
<!-- Child Views -->
</LinearLayout>
4. Inside your activity use AnimationDrawable to apply the transition.
LinearLayout container = (LinearLayout) findViewById(R.id.container);
AnimationDrawable anim = (AnimationDrawable) container.getBackground();
anim.setEnterFadeDuration(6000);
anim.setExitFadeDuration(2000);
5. Starting animation:- start the animation on onResume.
@Override
protected void onResume() {
super.onResume();
if (anim != null && !anim.isRunning())
anim.start();
}
6. Stopping animation:- stop the animation on onPause.
@Override
protected void onPause() {
super.onPause();
if (anim != null && anim.isRunning())
anim.stop();
}
/* * That's all you need to apply the gradient color animation transition.
* Above is just the example using 4 gradients.
* You can create as many gradients as you need.
* and can create the animation_list using that gradients.
* */
// --------------------------------------------------------------------------------------------------------------
Link to the sample app repo of above functionality
https://github.com/deepak786/InstagramLikeColorTransition
@andriaripratama
Copy link

Awesome! Thanks!

@Anupamskd7
Copy link

Anupamskd7 commented Jun 25, 2017

getting error --- anim - Qualifier must be an expression.

my code:

public class MainActivity extends AppCompatActivity {

@OverRide
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    AnimationDrawable anim = (AnimationDrawable) container.getBackground();
    anim.setEnterFadeDuration(6000);
    anim.setExitFadeDuration(2000);

}


@Override
public void onResume() {
    super.onResume();
    if (anim != null && !anim.isRunning())
        anim.start();
}

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

@Salmansz54
Copy link

no animation is showing

@joaquini
Copy link

Is there a way to avoid going to white before going to the next gradient?

@yusufpats
Copy link

Keep the duration for the exit fade to be a lot larger than the the enter fade duration.

@hama-Omer
Copy link

same here i have error of anim should we declare animation globally or what

@deepak786
Copy link
Author

@hama-Omer so you have to declare the variable anim outside of onCreate method.

Private AnimationDrawable anim;

@override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    anim = (AnimationDrawable) container.getBackground();
    anim.setEnterFadeDuration(6000);
    anim.setExitFadeDuration(2000);
}

@deepak786
Copy link
Author

Hello all,

i noticed that some users have issues while using this gist so i have decided to create a sample app of the above functionality.
will update you when the sample is ready.

@deepak786
Copy link
Author

Below is the link to sample app repo
https://github.com/deepak786/InstagramLikeColorTransition

@avinashgardas
Copy link

Good repo InstagramLikeColorTransition, keep going!

@aryalireza
Copy link

im using kotlin and i have problem yet !
i checked your new sample , and i declared anim before onCreate , but after that anim dosent have setEnterFadeDuration !!

@deepak786
Copy link
Author

@aryalireza Please check the sample at https://github.com/deepak786/InstagramLikeColorTransition
I have added the Kotlin module.

@irfanirawansukirman
Copy link

Wowsome, thanks bro

@innovativesahil
Copy link

Thanks It worked but I tweaked some color values and angle to make it more appealing

@Aniket204
Copy link

@innovativesahil mind showing us?

@Aniket204
Copy link

@deepak786 i did just that, but the app crashes whenever that activity is launched whose background is animation. reallyneed this please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment