android - How background changes at login in Instagram app? -


i have made app change background color every few milliseconds. not appealing user. if have seen instagram's login screen, changes color @ soft rate , blurry effect.

i wanted type of background app. should have that

using following code might start you:

public class backgroundpainter {    private static final int min = 800;   private static final int max = 1500;    private final random random;    public backgroundpainter() {     random = new random();   }    public void animate(@nonnull final view target, @colorint final int color1,       @colorint final int color2) {      final valueanimator valueanimator = valueanimator.ofobject(new argbevaluator(), color1, color2);      valueanimator.setduration(randint(min, max));      valueanimator.addupdatelistener(new valueanimator.animatorupdatelistener() {       @override public void onanimationupdate(valueanimator animation) {         target.setbackgroundcolor((int) animation.getanimatedvalue());       }     });     valueanimator.addlistener(new animatorlisteneradapter() {       @override public void onanimationend(animator animation) {         //reverse animation         animate(target, color2, color1);       }     });      valueanimator.setinterpolator(new acceleratedecelerateinterpolator());     valueanimator.start();   }    private int randint(int min, int max) {     return random.nextint((max - min) + 1) + min;   } } 

with usage:

final view targetview = findviewbyid(r.id.root_view);  backgroundpainter backgroundpainter = new backgroundpainter();  int color1 = contextcompat.getcolor(this, r.color.coloraccent); int color2 = contextcompat.getcolor(this, r.color.colorprimary);  backgroundpainter.animate(targetview, color1, color2); 

update

for changing background consists of more 1 color, speaking of drawables instead of valueanimator can try using below solution.

i have tested code on device api 19 , 23.

define colors in colors.xml:

  <color name="color1">#9c27b0</color>   <color name="color2">#ff4081</color>   <color name="color3">#7b1fa2</color>   <color name="color4">#f8bbd0</color>   <color name="color5">#ff5252</color>   <color name="color6">#607d8b</color>   <color name="color7">#ff5722</color>   <color name="color8">#ffa000</color> 

define gradients in drawable:

gradient_1.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">   <gradient       android:angle="45"       android:endcolor="@color/color2"       android:startcolor="@color/color1"       android:type="linear"/> </shape> 

gradient_2.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">   <gradient       android:angle="-45"       android:endcolor="@color/color5"       android:startcolor="@color/color3"       android:type="linear"/> </shape> 

gradient_3.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">   <gradient       android:angle="45"       android:endcolor="@color/color8"       android:startcolor="@color/color7"       android:type="linear"/> </shape> 

create class gradientbackgroundpainter in project:

public class gradientbackgroundpainter {    private static final int min = 4000;   private static final int max = 5000;    private final random random;   private final handler handler;   private final view target;   private final int[] drawables;   private final context context;    public gradientbackgroundpainter(@nonnull view target, int[] drawables) {     this.target = target;     this.drawables = drawables;     random = new random();     handler = new handler();     context = target.getcontext().getapplicationcontext();   }    private void animate(final int firstdrawable, int seconddrawable, final int duration) {     if (seconddrawable >= drawables.length) {       seconddrawable = 0;     }     final drawable first = contextcompat.getdrawable(context, drawables[firstdrawable]);     final drawable second = contextcompat.getdrawable(context, drawables[seconddrawable]);      final transitiondrawable transitiondrawable =         new transitiondrawable(new drawable[] { first, second });      target.setbackgrounddrawable(transitiondrawable);      transitiondrawable.setcrossfadeenabled(false);      transitiondrawable.starttransition(duration);      final int localseconddrawable = seconddrawable;     handler.postdelayed(new runnable() {       @override public void run() {         animate(localseconddrawable, localseconddrawable + 1, randint(min, max));       }     }, duration);   }    public void start() {     final int duration = randint(min, max);     animate(0, 1, duration);   }    public void stop() {     handler.removecallbacksandmessages(null);   }    private int randint(int min, int max) {     return random.nextint((max - min) + 1) + min;   } } 

and usage:

public class mainactivity extends appcompatactivity {    private gradientbackgroundpainter gradientbackgroundpainter;    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      view backgroundimage = findviewbyid(r.id.root_view);      final int[] drawables = new int[3];     drawables[0] = r.drawable.gradient_1;     drawables[1] = r.drawable.gradient_2;     drawables[2] = r.drawable.gradient_3;      gradientbackgroundpainter = new gradientbackgroundpainter(backgroundimage, drawables);     gradientbackgroundpainter.start();   }    @override protected void ondestroy() {     super.ondestroy();     gradientbackgroundpainter.stop();   } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -