1

I have been trying to find a way to change the background color of my relative layout gradually to all colors continuously for example ( soft blue, blue, navy blue, purple, and so on) the code that i have right now only changes the color gradually from black to white. I will be thankful for any help or advice that i may get.

this is the code that i have right now.

 layOut = (RelativeLayout)findViewById(R.id.relativeLayout2);



new Thread() {
   int color = 0;
      public void run() {
            for (color = 0; color < 255; color++) {
                try {
                    sleep(20);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            layOut.setBackgroundColor(Color.argb(255,
                                    color, color, color));
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();'`enter code here
Kalyan Vedala
  • 1,049
  • 2
  • 17
  • 33
user3689832
  • 31
  • 1
  • 2
  • 5
  • are you looking for this http://helpdesk.objects.com.au/java/changing-the-colormodel-of-a-bufferedimage –  Dec 27 '14 at 07:05

4 Answers4

1

try object animator for with color property, check this link, it more usefull for you.

color animation using object animator

Community
  • 1
  • 1
balaji koduri
  • 1,321
  • 9
  • 25
0

If I understand your question, change the red, green and blue color values. Something like,

public void run() {
    for (int red = 0; red < 256; red += 8) {
        for (int green = 0; green < 256; green += 8) {
            for (int blue = 0; blue < 256; blue += 8) {
                try {
                    sleep(20);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            layOut.setBackgroundColor(Color.argb(255,
                                red, green, blue));
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

When you set the three channels to the same value you get black (0), all the shades of grey and then white (255) (that's how color works).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I think those things are fun to implement. Youre just changing rgb values, so you don't have one, but three color variables. Example:

float fromRed = 0;
float fromGreen = 0;
float fromBlue = 0;

float toRed = 0;
float toGreen= 100;
float toBlue = 255;

float stepRed = (toRed - fromRead) / 30f;
float stepGreen= (toGreen - fromGreen) / 30f;
float stepBlue = (toBlue- fromBlue) / 30f;

for(float i = 0; i < 30; i++) {
    try {
        sleep(20);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layOut.setBackgroundColor(Color.argb(255,
                    fromRed + stepRed*i, fromGreen + stepGreen*i, fromBlue + stepBlue*i));
            }
         });
     } catch (InterruptedException e) {}
}

So you calculate the colors seperately and apply them (in 30 steps, in this case). Hope it works, haven't tested it because the emulator takes soooo long to load :(

brickBreaker
  • 339
  • 2
  • 10
0

You are changing the RGB all three at same time that makes color transition from black to white. Here is the code that I have tested.

I have used HSV to do this.(In RGB you need to maintain all the three variables like you have to add 3 loops). Here is the code.

rl = (RelativeLayout) findViewById(R.id.myRelativeLayout);


    new Thread() {
          int hue = 0 ; 
          int saturation = 240; //adjust as per your need
          int value = 120; //adjust as per your need

          public void run() {
                    for ( hue = 0; hue < 255; hue++) {
                        try {
                            sleep(20);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //Now form a hsv float array from the 3 variables
                                    float[] hsv = {hue , saturation , value};

                                    //make color from that float array
                                    int cl = Color.HSVToColor(hsv);

                                    rl.setBackgroundColor(cl);
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

Hope it will help you, feel free to ask any further queries.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • I tried it and it worked :). is there a way that i could make change without stopping at any color and just keep on going? – user3689832 Dec 28 '14 at 05:48
  • Okay you mean infinite looping...its simple just put that for loop inside of the one while loop as below while (true) { for (hue = 0; hue < 255; hue++) { .... } } – Vatsal Padhiyar Dec 28 '14 at 06:58