简体   繁体   English

Android:使用搜索栏为多个imageview背景逐渐更改颜色

[英]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 RG or B from the hex color. 我在imageview上使用背景色来制作矩形颜色,并且我想通过从十六进制颜色中删除RG或B来使用搜寻栏将其逐渐变形为较浅的颜色。 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 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. 根据您想要的颜色,将搜寻栏上的最大值设置为255,然后您就可以使用数字来逐渐获得所需的颜色。 Also great reference when trying to find the color http://www.tayloredmktg.com/rgb/ 尝试查找颜色时也很有参考价值http://www.tayloredmktg.com/rgb/

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. 根据您想要的颜色,将搜寻栏上的最大值设置为255,然后您就可以使用数字来逐渐获得所需的颜色。 Also great reference when trying to find the color http://www.tayloredmktg.com/rgb/ 尝试查找颜色时也很有参考价值http://www.tayloredmktg.com/rgb/

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM