goudarziha
goudarziha

Reputation: 336

Android: changing colors gradually for multiple imageview backgrounds using seekbar

I am using background color on an imageview to make a rectangle color and I want to to morph gradually to a lighter color using the seekbar by removing an R G or B from the hex color. How can I do this without writing a million lines of code? This is an example of what I want it to do, experimenting with one box though. https://d396qusza40orc.cloudfront.net/cmsc436/Labs/ModernArtUI/modernUI.mp4

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (i >= 25 && i < 50) {
                imageView.setBackgroundColor(Color.BLUE);
            } else if (i >= 50 && i < 75) {
                imageView.setBackgroundColor(Color.GREEN);
            } else if (i >= 75 && i <= 100) {
                imageView.setBackgroundColor(Color.YELLOW);
            } else {
                imageView.setBackgroundColor(Color.WHITE);
            }
        }

Answer found:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                iv1.setBackgroundColor(Color.argb(0xFF, i, i, 200));
}

Depending on what color you want, set the max on the seekbar to 255 and you can play around with the numbers to get your desired color at a gradual change. Also great reference when trying to find the color http://www.tayloredmktg.com/rgb/

Upvotes: 1

Views: 3266

Answers (2)

goudarziha
goudarziha

Reputation: 336

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            iv1.setBackgroundColor(Color.argb(0xFF, i, i, 200));
}

Depending on what color you want, set the max on the seekbar to 255 and you can play around with the numbers to get your desired color at a gradual change. Also great reference when trying to find the color http://www.tayloredmktg.com/rgb/

Upvotes: 4

Stephen
Stephen

Reputation: 4239

There is no single correct way. It depends on how you want to traverse the spectrum of color.

One option:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        float hue = i/100f;
        imageView.setBackgroundColor(Color.HSVToColor( new float[]{ hue, 1f, 0.75f } ) );
    }

Another (less graceful) option would be

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        double increment = 0xFFFFFFd / 100d;
        int color = i * increment;

        imageView.setBackgroundColor(color);
    }

Upvotes: 3

Related Questions