gradual button color change on android -


i want gradually change button color, after click on it. mean, button must have, example next set of colors: default - dark dark blue, dark blue, blue, light blue, , in end - lightest blue. example, want change button color in cycle, in next code. but, can't understand, why doesn't show intermediate colors. shows first color, , last one.

how improve this?

public class activityexample extends activity { private changecolorbtn;  @override protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_animations);      changecolorbtn = (button) findviewbyid(r.id.btn_change_color);      changecolorbtn.setbackgroundcolor(color.black);     changecolorbtn.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             changebuttoncolor(v);          }     });  }  private void changebuttoncolor(view v) {     // how many intermediate color be, , delay in millisecond between them     int count = 20, delay = 100;     (int = 0; < count; i++) {         try {              int color = ((colordrawable) changecolorbtn.getbackground())                     .getcolor();             int blue = color.blue(color), red = color.red(color), green = color.green(color);             changecolorbtn.setbackgroundcolor(color.rgb(red+10, green+5, blue+3));              thread.sleep(delay);         } catch (interruptedexception ine) {         }     }  }  @override protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } 

}

i solved problem using transitiondrawable. can follow next step:

  • create xml file in drawable folder, , write there like:

`

<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@color/color1" />     <item android:drawable="@color/color2" /> </transition> 

`

  • then, in xml button (or element / view) should reference transitiondrawable in android:background attribute.
  • also should have colors stored resources: this, have create xml following:

`

<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="color1">#990000</color>     <color name="color2">#cc3311</color> </resources> 

`

and save xml file in /res/values/ folder, name xml color.xml.

  • and initiate transition in code:

`

int durationmillis = 2000; transitiondrawable transition = (transitiondrawable) changecolorbtn.getbackground(); transition.starttransition(durationmillis); 

`

this helped me, hope useful others.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -