View background color animation


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import android.graphics.drawable.ColorDrawable;
  2. import android.graphics.drawable.Drawable;
  3. import android.graphics.drawable.TransitionDrawable;
  4.  
  5. public class BGColorTransitionDrawable extends TransitionDrawable {
  6. private int interval;
  7. public BGColorTransitionDrawable(Drawable[] layers) {
  8. super(layers);
  9. interval = 500;
  10. initVars();
  11. }
  12. public BGColorTransitionDrawable(Drawable[] layers, int interval) {
  13. super(layers);
  14. this.interval = interval;
  15. initVars();
  16. }
  17. private void initVars(){
  18. setCrossFadeEnabled(true);
  19. setId(0,0);
  20. setId(1,1);
  21. }
  22. public void changeColor(int color){
  23. setDrawableByLayerId(0, getDrawable(1));
  24. setDrawableByLayerId(1, new ColorDrawable(color));
  25. startTransition(interval);
  26. }
  27.  
  28. }
  29.  
  30. I set and call it like so.
  31.  
  32. ColorDrawable layers[] = new ColorDrawable[2];
  33. layers[0] = new ColorDrawable(0xff0000ff);
  34. layers[1] = new ColorDrawable(0xffff0000);
  35. backgroundColorAnimation = new BGColorTransitionDrawable(layers);
  36. vColorSwapper.setBackgroundDrawable(backgroundColorAnimation);
  37.  
  38. backgroundColorAnimation.setColor(0xff00ff00);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.