スマートウォッチの比較サイト

最新のデバイスをできるだけ丁寧にレビューをしていきます!時間の都合もあるので、不定期になります。

Androidでランダムな背景色を設定する方法

Androidで動的に背景色を設定する方法を応用して、色をランダムにしてみます。

 

<Values/color.xml>

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
<color name="Navy">#000080</color>
<color name="Dark_Blue">#00008B</color>
<color name="Blue">#0000FF</color>
<color name="Medium_Blue">#0000CD</color>
<color name="Dark_Green">#006400</color>
<color name="Teal">#008080</color>
<color name="Green">#008000</color>
<color name="Dark_Cyan">#008B8B</color>
<color name="Deep_Sky_Blue">#00BFFF</color>
<color name="Dark_Turquoise">#00CED1</color>
<color name="Medium_Spring_Green">#00FA9A</color>
<color name="Lime">#00FF00</color>
<color name="Spring_Green">#00FF7F</color>
<color name="Aqua">#00FFFF</color>
<color name="Midnight_Blue">#191970</color>
<color name="Dodger_Blue">#1E90FF</color>
<color name="Light_Sea_Green">#20B2AA</color>
<color name="Forest_Green">#228B22</color>
<color name="Sea_Green">#2E8B57</color>
<color name="Dark_Slate_Gray">#2F4F4F</color>
<color name="Lime_Green">#32CD32</color>
<color name="Medium_Sea_Green">#3CB371</color>
<color name="Turquoise">#40E0D0</color>
<color name="Royal_Blue">#4169E1</color>
<color name="Steel_Blue">#4682B4</color>
<color name="Dark_Slate_Blue">#483D8B</color>
<color name="Medium_Turquoise">#48D1CC</color>
<color name="Indigo">#4B0082</color>
<color name="Dark_Olive_Green">#556B2F</color>
<color name="Cadet_Blue">#5F9EA0</color>
<color name="Corn_Flower_Blue">#6495ED</color>
<color name="MediumAquamarine">#66CDAA</color>
<color name="Dim_Gray">#696969</color>
<color name="Slate_Blue">#6A5ACD</color>
<color name="Olive_Drab">#6B8E23</color>
<color name="Slate_Gray">#708090</color>
<color name="Light_Slate_Gray">#778899</color>
<color name="Medium_Slate_Blue">#7B68EE</color>
<color name="Lawn_Green">#7CFC00</color>
<color name="Chartreuse">#7FFF00</color>
<color name="Aquamarine">#7FFFD4</color>
<color name="Maroon">#800000</color>
<color name="Olive">#808000</color>
<color name="Sky_Blue">#87CEEB</color>
<color name="Light_Sky_Blue">#87CEFA</color>
<color name="Blue_Violet">#8A2BE2</color>
<color name="Dark_Red">#8B0000</color>
<color name="Dark_Magenta">#8B008B</color>
<color name="SaddleBrown">#8B4513</color>
<color name="Dark_Sea_Green">#8FBC8F</color>
<color name="Light_Green">#90EE90</color>
<color name="Medium_Purple">#9370DB</color>
<color name="Dark_Violet">#9400D3</color>
<color name="Pale_Green">#98FB98</color>
<color name="DarkOrchid">#9932CC</color>
<color name="Yellow_Green">#9ACD32</color>
<color name="Sienna">#A0522D</color>
<color name="Brown">#A52A2A</color>
<color name="Dark_Gray">#A9A9A9</color>
<color name="Light_Blue">#ADD8E6</color>
<color name="Green_Yellow">#ADFF2F</color>
<color name="Pale_Turquoise">#AFEEEE</color>
<color name="Light_Steel_Blue">#B0C4DE</color>
<color name="Powder_Blue">#B0E0E6</color>
<color name="Fire_Brick">#B22222</color>
<color name="Dark_Goldenrod">#B8860B</color>
<color name="Medium_Orchid">#BA55D3</color>
<color name="Rosy_Brown">#BC8F8F</color>
<color name="Dark_Khaki">#BDB76B</color>
<color name="Silver">#C0C0C0</color>
<color name="Medium_Violet_Red">#C71585</color>
<color name="Indian_Red">#CD5C5C</color>
<color name="Peru">#CD853F</color>
<color name="Chocolate">#D2691E</color>
<color name="Tan">#D2B48C</color>
<color name="Light_Gray">#D3D3D3</color>
<color name="Thistle">#D8BFD8</color>
<color name="Orchid">#DA70D6</color>
<color name="Goldenrod">#DAA520</color>
<color name="Pale_Violet_Red">#DB7093</color>
<color name="Crimson">#DC143C</color>
<color name="Gains_Boro">#DCDCDC</color>
<color name="Plum">#DDA0DD</color>
<color name="BurlyWood">#DEB887</color>
<color name="Light_Cyan">#E0FFFF</color>
<color name="Lavender">#E6E6FA</color>
<color name="Dark_Salmon">#E9967A</color>
<color name="Violet">#EE82EE</color>
<color name="Pale_Golden_Rod">#EEE8AA</color>
<color name="Light_Coral">#F08080</color>
<color name="Khaki">#F0E68C</color>
<color name="Alice_Blue">#F0F8FF</color>
<color name="Honeydew">#F0FFF0</color>
<color name="Azure">#F0FFFF</color>
<color name="Sandy_Brown">#F4A460</color>
<color name="Wheat">#F5DEB3</color>
<color name="Beige">#F5F5DC</color>
<color name="White_Smoke">#F5F5F5</color>
<color name="Mint_Cream">#F5FFFA</color>
<color name="Ghost_White">#F8F8FF</color>
<color name="Salmon">#FA8072</color>
<color name="Antique_White">#FAEBD7</color>
<color name="Linen">#FAF0E6</color>
<color name="Light_Golden_Rod_Yellow">#FAFAD2</color>
<color name="Oldlace">#FDF5E6</color>
<color name="Red">#FF0000</color>
<color name="Fuchsia">#FF00FF</color>
<color name="Magenta">#FF00FF</color>
<color name="Deep_Pink">#FF1493</color>
<color name="Orange_Red">#FF4500</color>
<color name="Tomato">#FF6347</color>
<color name="HotPink">#FF69B4</color>
<color name="Coral">#FF7F50</color>
<color name="Dark_Orange">#FF8C00</color>
<color name="Light_Salmon">#FFA07A</color>
<color name="Orange">#FFA500</color>
<color name="Light_Pink">#FFB6C1</color>
<color name="Pink">#FFC0CB</color>
<color name="Gold">#FFD700</color>
<color name="Peach_Puff">#FFDAB9</color>
<color name="Navajo_White">#FFDEAD</color>
<color name="Moccasin">#FFE4B5</color>
<color name="Bisque">#FFE4C4</color>
<color name="Misty_Rose">#FFE4E1</color>
<color name="Blanche_Dalmond">#FFEBCD</color>
<color name="Papayawhip">#FFEFD5</color>
<color name="Lavender_Blush">#FFF0F5</color>
<color name="Sea_Shall">#FFF5EE</color>
<color name="Corn_Silk">#FFF8DC</color>
<color name="Lemon_Chiffon">#FFFACD</color>
<color name="Floral_White">#FFFAF0</color>
<color name="Snow">#FFFAFA</color>
<color name="Yellow">#FFFF00</color>
<color name="Light_Yellow">#FFFFE0</color>
<color name="Ivory">#FFFFF0</color>
<color name="White">#FFFFFF</color>
</resources>

 

<ViewColor.java>

public class ViewColor {
int hairetu3 = new int{
R.color.Black, R.color.Navy, R.color.Dark_Blue, R.color.Blue, R.color.Medium_Blue,
R.color.Dark_Green, R.color.Teal, R.color.Green, R.color.Dark_Cyan, R.color.Deep_Sky_Blue,
R.color.Dark_Turquoise, R.color.Medium_Spring_Green, R.color.Lime, R.color.Spring_Green,
R.color.Aqua, R.color.Midnight_Blue, R.color.Dodger_Blue, R.color.Light_Sea_Green, R.color.Forest_Green,
R.color.Sea_Green, R.color.Dark_Slate_Gray, R.color.Lime_Green, R.color.Medium_Sea_Green, R.color.Turquoise,
R.color.Royal_Blue, R.color.Steel_Blue, R.color.Dark_Slate_Blue, R.color.Medium_Turquoise, R.color.Indigo,
R.color.Dark_Olive_Green, R.color.Cadet_Blue, R.color.Corn_Flower_Blue, R.color.Medium_Aquamarine, R.color.Dim_Gray,
R.color.Slate_Blue, R.color.Olive_Drab, R.color.Slate_Gray, R.color.Light_Slate_Gray, R.color.Medium_Slate_Blue,
R.color.Lawn_Green, R.color.Chartreuse, R.color.Aquamarine, R.color.Maroon, R.color.Olive,
R.color.Sky_Blue, R.color.Light_Sky_Blue, R.color.Blue_Violet, R.color.Dark_Red, R.color.Dark_Magenta,
R.color.Saddle_Brown, R.color.Dark_Sea_Green, R.color.Light_Green, R.color.Medium_Purple,
R.color.Dark_Violet, R.color.Pale_Green, R.color.Dark_Orchid, R.color.Yellow_Green, R.color.Sienna,
R.color.Brown, R.color.Dark_Gray, R.color.Light_Blue, R.color.Green_Yellow, R.color.Pale_Turquoise,
R.color.Light_Steel_Blue, R.color.Powder_Blue, R.color.Fire_Brick, R.color.Dark_Goldenrod,
R.color.Medium_Orchid, R.color.Rosy_Brown, R.color.Dark_Khaki, R.color.Silver, R.color.Medium_Violet_Red,
R.color.Indian_Red, R.color.Peru, R.color.Chocolate, R.color.Tan, R.color.Light_Gray, R.color.Thistle,
R.color.Orchid, R.color.Goldenrod, R.color.Pale_Violet_Red, R.color.Crimson, R.color.Gains_Boro,
R.color.Plum, R.color.Burly_Wood, R.color.Light_Cyan, R.color.Lavender, R.color.Dark_Salmon,
R.color.Violet, R.color.Pale_Golden_Rod, R.color.Light_Coral, R.color.Khaki, R.color.Alice_Blue,
R.color.Honeydew, R.color.Azure, R.color.Sandy_Brown, R.color.Wheat, R.color.Beige, R.color.White_Smoke,
R.color.Mint_Cream, R.color.Ghost_White, R.color.Salmon, R.color.Antique_White, R.color.Linen,
R.color.Light_Golden_Rod_Yellow, R.color.Oldlace, R.color.Red, R.color.Fuchsia, R.color.Magenta,
R.color.Deep_Pink, R.color.Orange_Red, R.color.Tomato, R.color.Hot_Pink, R.color.Coral,
R.color.Dark_Salmon, R.color.Dark_Orange, R.color.Light_Salmon, R.color.Orange, R.color.Light_Pink,
R.color.Pink, R.color.Gold, R.color.Peach_Puff, R.color.Navajo_White, R.color.Moccasin, R.color.Bisque,
R.color.Misty_Rose, R.color.Blanche_Dalmond, R.color.Papayawhip, R.color.Lavender_Blush, R.color.Sea_Shall,
R.color.Corn_Silk, R.color.Lemon_Chiffon, R.color.Floral_White, R.color.Snow, R.color.Yellow,
R.color.Light_Yellow, R.color.Ivory, R.color.White};

int getRandomColor() {
return hairetu3[(int)(Math.random() * hairetu3.length)];
}
}

 

<MainActivity.java>

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

View view = (View)findViewById(R.id."
viewのid");
ViewColor viewColor = new ViewColor();
view.setBackgroundResource(
vc.getRandomColor());
}

view.setBackground
Resourse(vc.getRandomColor());はsetBackgroundColorでもOKです。

解説

color.xmlで16進数カラーコードから色のリストを作り、ViewColor.javaでリストを並べて、そこからgetRandomColor()で色のリストからランダムに一つ色を取り出します。

そしてMainActivityのonCreate()でViewを関連付け、ViewColorをオブジェクトとして生成して、getRandomColorを背景にしてやってます。

わからなかったら気軽にコメントしていってくださいね♪
広告を非表示にする