【问题标题】:Gradually change background color to all colors continuously逐渐将背景颜色连续更改为所有颜色
【发布时间】:2015-02-24 05:44:07
【问题描述】:

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

这是我现在拥有的代码。

 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

【问题讨论】:

标签: java android android-layout development-environment


【解决方案1】:

您正在同时更改所有三个 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();

希望对您有所帮助,如有任何疑问,请随时提出。

【讨论】:

  • 我试过了,它奏效了:)。有没有一种方法可以让我在不停留任何颜色的情况下进行更改并继续前进?
  • 好吧,你的意思是无限循环......它很简单,只需将 for 循环放在一个 while 循环中,如下所示 while (true) { for (hue = 0; hue
  • 我真的很感激thanx
【解决方案2】:

我认为实现这些东西很有趣。您只是在更改 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 步)。希望它可以工作,还没有测试它,因为模拟器需要很长时间才能加载:(

【讨论】:

    【解决方案3】:

    尝试使用颜色属性的对象动画器,检查此链接,它对您更有用。

    color animation using object animator

    【讨论】:

      【解决方案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)(这就是颜色的工作原理)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-10
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-03
        • 1970-01-01
        相关资源
        最近更新 更多