android - I want to use random colors for background with smooth transition effect in loop -
my problem instead of getting random colors @ background getting 2 colors animation effect background. , when restart app 2 color changes. goal there must random colors use background smooth animation of color change here's code
public class mainactivity extends appcompatactivity { int color1,color2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final relativelayout targetview = (relativelayout) findviewbyid(r.id.new2); backgroundpainter backgroundpainter = new backgroundpainter(); color1=getrandcolor(); color2=getrandcolor(); backgroundpainter.animate(targetview, color1, color2); // int color1 = contextcompat.getcolor(this, r.color.coloraccent); //int color2 = contextcompat.getcolor(this, r.color.colorprimary); } public int getrandcolor(){ random rand = new random(); int r = rand.nextint(255); int g = rand.nextint(255); int b = rand.nextint(255); int rando = color.rgb(r, g, b); return rando; } public class backgroundpainter { private static final int min = 1800; private static final int max = 2300; 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; } } }
and main xml file
<?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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="weathercheck.bt4u.com.myapplication.mainactivity" android:id="@+id/screen" android:orientation="horizontal"> </linearlayout>
just change
animate(target, color2, color1);
to
animate(target, color2, getrandcolor());
Comments
Post a Comment