有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不断地将背景颜色逐渐更改为所有颜色

我一直在试图找到一种方法,将我相对布局的背景颜色逐渐更改为所有颜色,例如(软蓝色、蓝色、海军蓝、紫色等)。我现在的代码只是将颜色从黑色逐渐更改为白色。我将感谢任何可能得到的帮助或建议

这是我现在掌握的代码

 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

共 (4) 个答案

  1. # 1 楼答案

    我认为这些事情实施起来很有趣。你只是在改变rgb值,所以你没有一个,只有三个颜色变量。例如:

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

    因此,你可以分别计算颜色并应用它们(在本例中,分30个步骤)。希望它能工作,但还没有测试它,因为模拟器需要很长时间才能加载:(

  2. # 3 楼答案

    同时更改RGB,使颜色从黑色过渡到白色。下面是我已经测试过的代码

    我用HSV来做这个。(在RGB中,您需要维护所有三个变量,就像必须添加3个循环一样)。这是代码

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

    希望它能帮助您,请随时提出任何进一步的疑问

  3. # 4 楼答案

    如果我理解您的问题,请更改redgreenblue颜色值。比如

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

    当你将三个通道设置为相同的值时,你会得到黑色(0),所有的灰色,然后是白色(255)(这就是颜色的工作原理)