2014-12-27 113 views
0

我一直試圖找到一種方式來逐步改變我的相對佈局的背景色爲所有的顏色不斷例如(軟藍,藍色,寶藍色,紫色,等等)我現在所擁有的代碼只會將顏色從黑色逐漸變爲白色。我會感謝任何我可能得到的幫助或建議。逐漸改變背景顏色,所有的顏色不斷

這是我現在的代碼。

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 
+0

你在找這個http://helpdesk.objects。 com.au/java/changing-the-colormodel-of-a-bufferedimage –

回答

0

如果我明白你的問題,改變red,​​和blue顏色值。喜歡的東西,

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)(這是怎樣的色彩作品)。

+0

謝謝你幫我出 – user3689832

0

我覺得那些東西是有趣的實現。你只是改變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步)。希望工程,因爲模擬器需要SOOOO長時間載入:(

+0

感謝你的幫助 – user3689832

0

您正在改變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(); 

希望它會幫助你,隨時問任何進一步的查詢。

+0

我想它和它的工作:)。有沒有一種方法可以在不停止任何顏色的情況下做出改變,並繼續前進? – user3689832

+0

好你的意思是無限循環...其簡單的只是把該for循環之一的內部,而環路而(真){ \t \t \t \t \t對於低於(色調= 0;色調<255;色調++){.. ..}} –

+0

謝謝@Vatsal它工作:) – user3689832