如何随机获得 Material Design 颜色?
Posted
技术标签:
【中文标题】如何随机获得 Material Design 颜色?【英文标题】:How to randomly receive a Material Design Color? 【发布时间】:2015-12-15 19:10:52 【问题描述】:Google 有他们的Color - Guidelines,那么如何随机接收颜色呢?有没有办法从表格中指定一个数字并从表格的所有颜色中接收一个随机颜色?
例如getMatColor(100);
:
编辑 - 结果: (图标颜色)
【问题讨论】:
【参考方案1】:迟到总比不到好。
如果真正的目的是为 type 颜色获取随机材料颜色,我的新回应。
添加 array.xml 文件(来自@8m47x)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="mdcolor_50">
<item name="red_50" type="color">#fde0dc</item>
<item name="pink_50" type="color">#fce4ec</item>
<item name="purple_50" type="color">#f3e5f5</item>
<item name="deep_purple_50" type="color">#ede7f6</item>
<item name="indigo_50" type="color">#e8eaf6</item>
<item name="blue_50" type="color">#e7e9fd</item>
<item name="light_blue_50" type="color">#e1f5fe</item>
<item name="cyan_50" type="color">#e0f7fa</item>
<item name="teal_50" type="color">#e0f2f1</item>
<item name="green_50" type="color">#d0f8ce</item>
<item name="light_green_50" type="color">#f1f8e9</item>
<item name="lime_50" type="color">#f9fbe7</item>
<item name="yellow_50" type="color">#fffde7</item>
<item name="amber_50" type="color">#fff8e1</item>
<item name="orange_50" type="color">#fff3e0</item>
<item name="deep_orange_50" type="color">#fbe9e7</item>
<item name="brown_50" type="color">#efebe9</item>
<item name="grey_50" type="color">#fafafa</item>
<item name="blue_grey_50" type="color">#eceff1</item>
</array>
<array name="mdcolor_100">
<item name="red_100" type="color">#f9bdbb</item>
<item name="pink_100" type="color">#f8bbd0</item>
<item name="purple_100" type="color">#e1bee7</item>
<item name="deep_purple_100" type="color">#d1c4e9</item>
<item name="indigo_100" type="color">#c5cae9</item>
<item name="blue_100" type="color">#d0d9ff</item>
<item name="light_blue_100" type="color">#b3e5fc</item>
<item name="cyan_100" type="color">#b2ebf2</item>
<item name="teal_100" type="color">#b2dfdb</item>
<item name="green_100" type="color">#a3e9a4</item>
<item name="light_green_100" type="color">#dcedc8</item>
<item name="lime_100" type="color">#f0f4c3</item>
<item name="yellow_100" type="color">#fff9c4</item>
<item name="amber_100" type="color">#ffecb3</item>
<item name="orange_100" type="color">#ffe0b2</item>
<item name="deep_orange_100" type="color">#ffccbc</item>
<item name="brown_100" type="color">#d7ccc8</item>
<item name="grey_100" type="color">#f5f5f5</item>
<item name="blue_grey_100" type="color">#cfd8dc</item>
</array>
<array name="mdcolor_200">
<item name="red_200" type="color">#f69988</item>
<item name="pink_200" type="color">#f48fb1</item>
<item name="purple_200" type="color">#ce93d8</item>
<item name="deep_purple_200" type="color">#b39ddb</item>
<item name="indigo_200" type="color">#9fa8da</item>
<item name="blue_200" type="color">#afbfff</item>
<item name="light_blue_200" type="color">#81d4fa</item>
<item name="cyan_200" type="color">#80deea</item>
<item name="teal_200" type="color">#80cbc4</item>
<item name="green_200" type="color">#72d572</item>
<item name="light_green_200" type="color">#c5e1a5</item>
<item name="lime_200" type="color">#e6ee9c</item>
<item name="yellow_200" type="color">#fff59d</item>
<item name="orange_200" type="color">#ffcc80</item>
<item name="deep_orange_200" type="color">#ffab91</item>
<item name="brown_200" type="color">#bcaaa4</item>
<item name="grey_200" type="color">#eeeeee</item>
<item name="blue_grey_200" type="color">#b0bec5</item>
</array>
<array name="mdcolor_300">
<item name="red_300" type="color">#f36c60</item>
<item name="pink_300" type="color">#f06292</item>
<item name="purple_300" type="color">#ba68c8</item>
<item name="deep_purple_300" type="color">#9575cd</item>
<item name="indigo_300" type="color">#7986cb</item>
<item name="blue_300" type="color">#91a7ff</item>
<item name="light_blue_300" type="color">#4fc3f7</item>
<item name="cyan_300" type="color">#4dd0e1</item>
<item name="teal_300" type="color">#4db6ac</item>
<item name="green_300" type="color">#42bd41</item>
<item name="light_green_300" type="color">#aed581</item>
<item name="lime_300" type="color">#dce775</item>
<item name="yellow_300" type="color">#fff176</item>
<item name="orange_300" type="color">#ffb74d</item>
<item name="deep_orange_300" type="color">#ff8a65</item>
<item name="brown_300" type="color">#a1887f</item>
<item name="grey_300" type="color">#e0e0e0</item>
<item name="blue_grey_300" type="color">#90a4ae</item>
</array>
<array name="mdcolor_400">
<item name="red_400" type="color">#e84e40</item>
<item name="pink_400" type="color">#ec407a</item>
<item name="purple_400" type="color">#ab47bc</item>
<item name="deep_purple_400" type="color">#7e57c2</item>
<item name="indigo_400" type="color">#5c6bc0</item>
<item name="blue_400" type="color">#738ffe</item>
<item name="light_blue_400" type="color">#29b6f6</item>
<item name="cyan_400" type="color">#26c6da</item>
<item name="teal_400" type="color">#26a69a</item>
<item name="green_400" type="color">#2baf2b</item>
<item name="light_green_400" type="color">#9ccc65</item>
<item name="lime_400" type="color">#d4e157</item>
<item name="yellow_400" type="color">#ffee58</item>
<item name="orange_400" type="color">#ffa726</item>
<item name="deep_orange_400" type="color">#ff7043</item>
<item name="brown_400" type="color">#8d6e63</item>
<item name="grey_400" type="color">#bdbdbd</item>
<item name="blue_grey_400" type="color">#78909c</item>
</array>
<array name="mdcolor_500">
<item name="red_500" type="color">#e51c23</item>
<item name="pink_500" type="color">#e91e63</item>
<item name="purple_500" type="color">#9c27b0</item>
<item name="deep_purple_500" type="color">#673ab7</item>
<item name="indigo_500" type="color">#3f51b5</item>
<item name="blue_500" type="color">#5677fc</item>
<item name="light_blue_500" type="color">#03a9f4</item>
<item name="cyan_500" type="color">#00bcd4</item>
<item name="teal_500" type="color">#009688</item>
<item name="green_500" type="color">#259b24</item>
<item name="light_green_500" type="color">#8bc34a</item>
<item name="lime_500" type="color">#cddc39</item>
<item name="yellow_500" type="color">#ffeb3b</item>
<item name="orange_500" type="color">#ff9800</item>
<item name="deep_orange_500" type="color">#ff5722</item>
<item name="brown_500" type="color">#795548</item>
<item name="grey_500" type="color">#9e9e9e</item>
<item name="blue_grey_500" type="color">#607d8b</item>
</array>
<array name="mdcolor_600">
<item name="red_600" type="color">#dd191d</item>
<item name="pink_600" type="color">#d81b60</item>
<item name="purple_600" type="color">#8e24aa</item>
<item name="deep_purple_600" type="color">#5e35b1</item>
<item name="indigo_600" type="color">#3949ab</item>
<item name="blue_600" type="color">#4e6cef</item>
<item name="light_blue_600" type="color">#039be5</item>
<item name="cyan_600" type="color">#00acc1</item>
<item name="teal_600" type="color">#00897b</item>
<item name="green_600" type="color">#0a8f08</item>
<item name="light_green_600" type="color">#7cb342</item>
<item name="lime_600" type="color">#c0ca33</item>
<item name="yellow_600" type="color">#fdd835</item>
<item name="orange_600" type="color">#fb8c00</item>
<item name="deep_orange_600" type="color">#f4511e</item>
<item name="brown_600" type="color">#6d4c41</item>
<item name="grey_600" type="color">#757575</item>
<item name="blue_grey_600" type="color">#546e7a</item>
</array>
<array name="mdcolor_700">
<item name="red_700" type="color">#d01716</item>
<item name="pink_700" type="color">#c2185b</item>
<item name="purple_700" type="color">#7b1fa2</item>
<item name="deep_purple_700" type="color">#512da8</item>
<item name="indigo_700" type="color">#303f9f</item>
<item name="blue_700" type="color">#455ede</item>
<item name="light_blue_700" type="color">#0288d1</item>
<item name="cyan_700" type="color">#0097a7</item>
<item name="teal_700" type="color">#00796b</item>
<item name="green_700" type="color">#0a7e07</item>
<item name="light_green_700" type="color">#689f38</item>
<item name="lime_700" type="color">#afb42b</item>
<item name="yellow_700" type="color">#fbc02d</item>
<item name="orange_700" type="color">#f57c00</item>
<item name="deep_orange_700" type="color">#e64a19</item>
<item name="brown_700" type="color">#5d4037</item>
<item name="grey_700" type="color">#616161</item>
<item name="blue_grey_700" type="color">#455a64</item>
</array>
<array name="mdcolor_800">
<item name="red_800" type="color">#c41411</item>
<item name="pink_800" type="color">#ad1457</item>
<item name="purple_800" type="color">#6a1b9a</item>
<item name="deep_purple_800" type="color">#4527a0</item>
<item name="indigo_800" type="color">#283593</item>
<item name="blue_800" type="color">#3b50ce</item>
<item name="light_blue_800" type="color">#0277bd</item>
<item name="cyan_800" type="color">#00838f</item>
<item name="teal_800" type="color">#00695c</item>
<item name="green_800" type="color">#056f00</item>
<item name="light_green_800" type="color">#558b2f</item>
<item name="lime_800" type="color">#9e9d24</item>
<item name="yellow_800" type="color">#f9a825</item>
<item name="orange_800" type="color">#ef6c00</item>
<item name="deep_orange_800" type="color">#d84315</item>
<item name="brown_800" type="color">#4e342e</item>
<item name="grey_800" type="color">#424242</item>
<item name="blue_grey_800" type="color">#37474f</item>
</array>
<array name="mdcolor_900">
<item name="red_900" type="color">#b0120a</item>
<item name="pink_900" type="color">#880e4f</item>
<item name="purple_900" type="color">#4a148c</item>
<item name="deep_purple_900" type="color">#311b92</item>
<item name="indigo_900" type="color">#1a237e</item>
<item name="blue_900" type="color">#2a36b1</item>
<item name="light_blue_900" type="color">#01579b</item>
<item name="cyan_900" type="color">#006064</item>
<item name="teal_900" type="color">#004d40</item>
<item name="green_900" type="color">#0d5302</item>
<item name="light_green_900" type="color">#33691e</item>
<item name="lime_900" type="color">#827717</item>
<item name="yellow_900" type="color">#f57f17</item>
<item name="orange_900" type="color">#e65100</item>
<item name="deep_orange_900" type="color">#bf360c</item>
<item name="brown_900" type="color">#3e2723</item>
<item name="grey_900" type="color">#212121</item>
<item name="blue_grey_900" type="color">#263238</item>
</array>
<array name="mdcolor_A100">
<item name="red_A100" type="color">#ff7997</item>
<item name="pink_A100" type="color">#ff80ab</item>
<item name="purple_A100" type="color">#ea80fc</item>
<item name="deep_purple_A100" type="color">#b388ff</item>
<item name="indigo_A100" type="color">#8c9eff</item>
<item name="blue_A100" type="color">#a6baff</item>
<item name="light_blue_A100" type="color">#80d8ff</item>
<item name="cyan_A100" type="color">#84ffff</item>
<item name="teal_A100" type="color">#a7ffeb</item>
<item name="green_A100" type="color">#a2f78d</item>
<item name="light_green_A100" type="color">#ccff90</item>
<item name="lime_A100" type="color">#f4ff81</item>
<item name="yellow_A100" type="color">#ffff8d</item>
<item name="orange_A100" type="color">#ffd180</item>
<item name="deep_orange_A100" type="color">#ff9e80</item>
</array>
<array name="mdcolor_A200">
<item name="red_A200" type="color">#ff5177</item>
<item name="pink_A200" type="color">#ff4081</item>
<item name="purple_A200" type="color">#e040fb</item>
<item name="deep_purple_A200" type="color">#7c4dff</item>
<item name="indigo_A200" type="color">#536dfe</item>
<item name="blue_A200" type="color">#6889ff</item>
<item name="light_blue_A200" type="color">#40c4ff</item>
<item name="cyan_A200" type="color">#18ffff</item>
<item name="teal_A200" type="color">#64ffda</item>
<item name="green_A200" type="color">#5af158</item>
<item name="light_green_A200" type="color">#b2ff59</item>
<item name="lime_A200" type="color">#eeff41</item>
<item name="yellow_A200" type="color">#ffff00</item>
<item name="orange_A200" type="color">#ffab40</item>
<item name="deep_orange_A200" type="color">#ff6e40</item>
</array>
<array name="mdcolor_A400">
<item name="red_A400" type="color">#ff2d6f</item>
<item name="pink_A400" type="color">#f50057</item>
<item name="purple_A400" type="color">#d500f9</item>
<item name="deep_purple_A400" type="color">#651fff</item>
<item name="indigo_A400" type="color">#3d5afe</item>
<item name="blue_A400" type="color">#4d73ff</item>
<item name="light_blue_A400" type="color">#00b0ff</item>
<item name="cyan_A400" type="color">#00e5ff</item>
<item name="teal_A400" type="color">#1de9b6</item>
<item name="green_A400" type="color">#14e715</item>
<item name="light_green_A400" type="color">#76ff03</item>
<item name="lime_A400" type="color">#c6ff00</item>
<item name="yellow_A400" type="color">#ffea00</item>
<item name="orange_A400" type="color">#ff9100</item>
<item name="deep_orange_A400" type="color">#ff3d00</item>
</array>
<array name="mdcolor_A700">
<item name="red_A700" type="color">#e00032</item>
<item name="pink_A700" type="color">#c51162</item>
<item name="purple_A700" type="color">#aa00ff</item>
<item name="deep_purple_A700" type="color">#6200ea</item>
<item name="indigo_A700" type="color">#304ffe</item>
<item name="blue_A700" type="color">#4d69ff</item>
<item name="light_blue_A700" type="color">#0091ea</item>
<item name="cyan_A700" type="color">#00b8d4</item>
<item name="teal_A700" type="color">#00bfa5</item>
<item name="green_A700" type="color">#12c700</item>
<item name="light_green_A700" type="color">#64dd17</item>
<item name="lime_A700" type="color">#aeea00</item>
<item name="yellow_A700" type="color">#ffd600</item>
<item name="orange_A700" type="color">#ff6d00</item>
<item name="deep_orange_A700" type="color">#dd2c00</item>
</array>
<array name="extra">
<item name="grey_1000b" type="color">#000000</item>
<item name="grey_1000w" type="color">#ffffff</item>
</array>
</resources>
添加以下方法以从父类型中选择随机颜色
private int getMatColor(String typeColor)
int returnColor = Color.BLACK;
int arrayId = getResources().getIdentifier("mdcolor_" + typeColor, "array", getApplicationContext().getPackageName());
if (arrayId != 0)
TypedArray colors = getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.BLACK);
colors.recycle();
return returnColor;
用法
txt.setTextColor(getMatColor("500"));
【讨论】:
顺便说一句,我手动对array.xml 进行了排序:) 我在*** 上询问了question 进行排序,但找不到有用的答案.. 你缺少琥珀。【参考方案2】:如果您想使用旧版 SDK,您可以尝试这种方式:
TypedArray colors = getResources().obtainTypedArray(R.array.loading_colors);
int index = (int) (Math.random() * colors .length());
int color = colors.getColor(index, Color.BLACK);
textView.setTextColor(color);
colors.recycle();
你需要在 arrays.xml 中声明你的颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="loading_colors">
<item name="red_50" type="color">#fde0dc</item>
<item name="red_100" type="color">#f9bdbb</item>
<item name="red_200" type="color">#f69988</item>
<item name="red_300" type="color">#f36c60</item>
<item name="red_400" type="color">#e84e40</item>
<item name="red_500" type="color">#e51c23</item>
<item name="red_600" type="color">#dd191d</item>
<item name="red_700" type="color">#d01716</item>
<item name="red_800" type="color">#c41411</item>
<item name="red_900" type="color">#b0120a</item>
<item name="red_A100" type="color">#ff7997</item>
<item name="red_A200" type="color">#ff5177</item>
<item name="red_A400" type="color">#ff2d6f</item>
<item name="red_A700" type="color">#e00032</item>
<item name="pink_50" type="color">#fce4ec</item>
<item name="pink_100" type="color">#f8bbd0</item>
<item name="pink_200" type="color">#f48fb1</item>
<item name="pink_300" type="color">#f06292</item>
<item name="pink_400" type="color">#ec407a</item>
<item name="pink_500" type="color">#e91e63</item>
<item name="pink_600" type="color">#d81b60</item>
<item name="pink_700" type="color">#c2185b</item>
<item name="pink_800" type="color">#ad1457</item>
<item name="pink_900" type="color">#880e4f</item>
<item name="pink_A100" type="color">#ff80ab</item>
<item name="pink_A200" type="color">#ff4081</item>
<item name="pink_A400" type="color">#f50057</item>
<item name="pink_A700" type="color">#c51162</item>
<item name="purple_50" type="color">#f3e5f5</item>
<item name="purple_100" type="color">#e1bee7</item>
<item name="purple_200" type="color">#ce93d8</item>
<item name="purple_300" type="color">#ba68c8</item>
<item name="purple_400" type="color">#ab47bc</item>
<item name="purple_500" type="color">#9c27b0</item>
<item name="purple_600" type="color">#8e24aa</item>
<item name="purple_700" type="color">#7b1fa2</item>
<item name="purple_800" type="color">#6a1b9a</item>
<item name="purple_900" type="color">#4a148c</item>
<item name="purple_A100" type="color">#ea80fc</item>
<item name="purple_A200" type="color">#e040fb</item>
<item name="purple_A400" type="color">#d500f9</item>
<item name="purple_A700" type="color">#aa00ff</item>
<item name="deep_purple_50" type="color">#ede7f6</item>
<item name="deep_purple_100" type="color">#d1c4e9</item>
<item name="deep_purple_200" type="color">#b39ddb</item>
<item name="deep_purple_300" type="color">#9575cd</item>
<item name="deep_purple_400" type="color">#7e57c2</item>
<item name="deep_purple_500" type="color">#673ab7</item>
<item name="deep_purple_600" type="color">#5e35b1</item>
<item name="deep_purple_700" type="color">#512da8</item>
<item name="deep_purple_800" type="color">#4527a0</item>
<item name="deep_purple_900" type="color">#311b92</item>
<item name="deep_purple_A100" type="color">#b388ff</item>
<item name="deep_purple_A200" type="color">#7c4dff</item>
<item name="deep_purple_A400" type="color">#651fff</item>
<item name="deep_purple_A700" type="color">#6200ea</item>
<item name="indigo_50" type="color">#e8eaf6</item>
<item name="indigo_100" type="color">#c5cae9</item>
<item name="indigo_200" type="color">#9fa8da</item>
<item name="indigo_300" type="color">#7986cb</item>
<item name="indigo_400" type="color">#5c6bc0</item>
<item name="indigo_500" type="color">#3f51b5</item>
<item name="indigo_600" type="color">#3949ab</item>
<item name="indigo_700" type="color">#303f9f</item>
<item name="indigo_800" type="color">#283593</item>
<item name="indigo_900" type="color">#1a237e</item>
<item name="indigo_A100" type="color">#8c9eff</item>
<item name="indigo_A200" type="color">#536dfe</item>
<item name="indigo_A400" type="color">#3d5afe</item>
<item name="indigo_A700" type="color">#304ffe</item>
<item name="blue_50" type="color">#e7e9fd</item>
<item name="blue_100" type="color">#d0d9ff</item>
<item name="blue_200" type="color">#afbfff</item>
<item name="blue_300" type="color">#91a7ff</item>
<item name="blue_400" type="color">#738ffe</item>
<item name="blue_500" type="color">#5677fc</item>
<item name="blue_600" type="color">#4e6cef</item>
<item name="blue_700" type="color">#455ede</item>
<item name="blue_800" type="color">#3b50ce</item>
<item name="blue_900" type="color">#2a36b1</item>
<item name="blue_A100" type="color">#a6baff</item>
<item name="blue_A200" type="color">#6889ff</item>
<item name="blue_A400" type="color">#4d73ff</item>
<item name="blue_A700" type="color">#4d69ff</item>
<item name="light_blue_50" type="color">#e1f5fe</item>
<item name="light_blue_100" type="color">#b3e5fc</item>
<item name="light_blue_200" type="color">#81d4fa</item>
<item name="light_blue_300" type="color">#4fc3f7</item>
<item name="light_blue_400" type="color">#29b6f6</item>
<item name="light_blue_500" type="color">#03a9f4</item>
<item name="light_blue_600" type="color">#039be5</item>
<item name="light_blue_700" type="color">#0288d1</item>
<item name="light_blue_800" type="color">#0277bd</item>
<item name="light_blue_900" type="color">#01579b</item>
<item name="light_blue_A100" type="color">#80d8ff</item>
<item name="light_blue_A200" type="color">#40c4ff</item>
<item name="light_blue_A400" type="color">#00b0ff</item>
<item name="light_blue_A700" type="color">#0091ea</item>
<item name="cyan_50" type="color">#e0f7fa</item>
<item name="cyan_100" type="color">#b2ebf2</item>
<item name="cyan_200" type="color">#80deea</item>
<item name="cyan_300" type="color">#4dd0e1</item>
<item name="cyan_400" type="color">#26c6da</item>
<item name="cyan_500" type="color">#00bcd4</item>
<item name="cyan_600" type="color">#00acc1</item>
<item name="cyan_700" type="color">#0097a7</item>
<item name="cyan_800" type="color">#00838f</item>
<item name="cyan_900" type="color">#006064</item>
<item name="cyan_A100" type="color">#84ffff</item>
<item name="cyan_A200" type="color">#18ffff</item>
<item name="cyan_A400" type="color">#00e5ff</item>
<item name="cyan_A700" type="color">#00b8d4</item>
<item name="teal_50" type="color">#e0f2f1</item>
<item name="teal_100" type="color">#b2dfdb</item>
<item name="teal_200" type="color">#80cbc4</item>
<item name="teal_300" type="color">#4db6ac</item>
<item name="teal_400" type="color">#26a69a</item>
<item name="teal_500" type="color">#009688</item>
<item name="teal_600" type="color">#00897b</item>
<item name="teal_700" type="color">#00796b</item>
<item name="teal_800" type="color">#00695c</item>
<item name="teal_900" type="color">#004d40</item>
<item name="teal_A100" type="color">#a7ffeb</item>
<item name="teal_A200" type="color">#64ffda</item>
<item name="teal_A400" type="color">#1de9b6</item>
<item name="teal_A700" type="color">#00bfa5</item>
<item name="green_50" type="color">#d0f8ce</item>
<item name="green_100" type="color">#a3e9a4</item>
<item name="green_200" type="color">#72d572</item>
<item name="green_300" type="color">#42bd41</item>
<item name="green_400" type="color">#2baf2b</item>
<item name="green_500" type="color">#259b24</item>
<item name="green_600" type="color">#0a8f08</item>
<item name="green_700" type="color">#0a7e07</item>
<item name="green_800" type="color">#056f00</item>
<item name="green_900" type="color">#0d5302</item>
<item name="green_A100" type="color">#a2f78d</item>
<item name="green_A200" type="color">#5af158</item>
<item name="green_A400" type="color">#14e715</item>
<item name="green_A700" type="color">#12c700</item>
<item name="light_green_50" type="color">#f1f8e9</item>
<item name="light_green_100" type="color">#dcedc8</item>
<item name="light_green_200" type="color">#c5e1a5</item>
<item name="light_green_300" type="color">#aed581</item>
<item name="light_green_400" type="color">#9ccc65</item>
<item name="light_green_500" type="color">#8bc34a</item>
<item name="light_green_600" type="color">#7cb342</item>
<item name="light_green_700" type="color">#689f38</item>
<item name="light_green_800" type="color">#558b2f</item>
<item name="light_green_900" type="color">#33691e</item>
<item name="light_green_A100" type="color">#ccff90</item>
<item name="light_green_A200" type="color">#b2ff59</item>
<item name="light_green_A400" type="color">#76ff03</item>
<item name="light_green_A700" type="color">#64dd17</item>
<item name="lime_50" type="color">#f9fbe7</item>
<item name="lime_100" type="color">#f0f4c3</item>
<item name="lime_200" type="color">#e6ee9c</item>
<item name="lime_300" type="color">#dce775</item>
<item name="lime_400" type="color">#d4e157</item>
<item name="lime_500" type="color">#cddc39</item>
<item name="lime_600" type="color">#c0ca33</item>
<item name="lime_700" type="color">#afb42b</item>
<item name="lime_800" type="color">#9e9d24</item>
<item name="lime_900" type="color">#827717</item>
<item name="lime_A100" type="color">#f4ff81</item>
<item name="lime_A200" type="color">#eeff41</item>
<item name="lime_A400" type="color">#c6ff00</item>
<item name="lime_A700" type="color">#aeea00</item>
<item name="yellow_50" type="color">#fffde7</item>
<item name="yellow_100" type="color">#fff9c4</item>
<item name="yellow_200" type="color">#fff59d</item>
<item name="yellow_300" type="color">#fff176</item>
<item name="yellow_400" type="color">#ffee58</item>
<item name="yellow_500" type="color">#ffeb3b</item>
<item name="yellow_600" type="color">#fdd835</item>
<item name="yellow_700" type="color">#fbc02d</item>
<item name="yellow_800" type="color">#f9a825</item>
<item name="yellow_900" type="color">#f57f17</item>
<item name="yellow_A100" type="color">#ffff8d</item>
<item name="yellow_A200" type="color">#ffff00</item>
<item name="yellow_A400" type="color">#ffea00</item>
<item name="yellow_A700" type="color">#ffd600</item>
<item name="amber_50" type="color">#fff8e1</item>
<item name="amber_100" type="color">#ffecb3</item>
<item name="amber_200" type="color">#ffe082</item>
<item name="amber_300" type="color">#ffd54f</item>
<item name="amber_400" type="color">#ffca28</item>
<item name="amber_500" type="color">#ffc107</item>
<item name="amber_600" type="color">#ffb300</item>
<item name="amber_700" type="color">#ffa000</item>
<item name="amber_800" type="color">#ff8f00</item>
<item name="amber_900" type="color">#ff6f00</item>
<item name="amber_A100" type="color">#ffe57f</item>
<item name="amber_A200" type="color">#ffd740</item>
<item name="amber_A400" type="color">#ffc400</item>
<item name="amber_A700" type="color">#ffab00</item>
<item name="orange_50" type="color">#fff3e0</item>
<item name="orange_100" type="color">#ffe0b2</item>
<item name="orange_200" type="color">#ffcc80</item>
<item name="orange_300" type="color">#ffb74d</item>
<item name="orange_400" type="color">#ffa726</item>
<item name="orange_500" type="color">#ff9800</item>
<item name="orange_600" type="color">#fb8c00</item>
<item name="orange_700" type="color">#f57c00</item>
<item name="orange_800" type="color">#ef6c00</item>
<item name="orange_900" type="color">#e65100</item>
<item name="orange_A100" type="color">#ffd180</item>
<item name="orange_A200" type="color">#ffab40</item>
<item name="orange_A400" type="color">#ff9100</item>
<item name="orange_A700" type="color">#ff6d00</item>
<item name="deep_orange_50" type="color">#fbe9e7</item>
<item name="deep_orange_100" type="color">#ffccbc</item>
<item name="deep_orange_200" type="color">#ffab91</item>
<item name="deep_orange_300" type="color">#ff8a65</item>
<item name="deep_orange_400" type="color">#ff7043</item>
<item name="deep_orange_500" type="color">#ff5722</item>
<item name="deep_orange_600" type="color">#f4511e</item>
<item name="deep_orange_700" type="color">#e64a19</item>
<item name="deep_orange_800" type="color">#d84315</item>
<item name="deep_orange_900" type="color">#bf360c</item>
<item name="deep_orange_A100" type="color">#ff9e80</item>
<item name="deep_orange_A200" type="color">#ff6e40</item>
<item name="deep_orange_A400" type="color">#ff3d00</item>
<item name="deep_orange_A700" type="color">#dd2c00</item>
<item name="brown_50" type="color">#efebe9</item>
<item name="brown_100" type="color">#d7ccc8</item>
<item name="brown_200" type="color">#bcaaa4</item>
<item name="brown_300" type="color">#a1887f</item>
<item name="brown_400" type="color">#8d6e63</item>
<item name="brown_500" type="color">#795548</item>
<item name="brown_600" type="color">#6d4c41</item>
<item name="brown_700" type="color">#5d4037</item>
<item name="brown_800" type="color">#4e342e</item>
<item name="brown_900" type="color">#3e2723</item>
<item name="grey_50" type="color">#fafafa</item>
<item name="grey_100" type="color">#f5f5f5</item>
<item name="grey_200" type="color">#eeeeee</item>
<item name="grey_300" type="color">#e0e0e0</item>
<item name="grey_400" type="color">#bdbdbd</item>
<item name="grey_500" type="color">#9e9e9e</item>
<item name="grey_600" type="color">#757575</item>
<item name="grey_700" type="color">#616161</item>
<item name="grey_800" type="color">#424242</item>
<item name="grey_900" type="color">#212121</item>
<item name="grey_1000b" type="color">#000000</item>
<item name="grey_1000w" type="color">#ffffff</item>
<item name="blue_grey_50" type="color">#eceff1</item>
<item name="blue_grey_100" type="color">#cfd8dc</item>
<item name="blue_grey_200" type="color">#b0bec5</item>
<item name="blue_grey_300" type="color">#90a4ae</item>
<item name="blue_grey_400" type="color">#78909c</item>
<item name="blue_grey_500" type="color">#607d8b</item>
<item name="blue_grey_600" type="color">#546e7a</item>
<item name="blue_grey_700" type="color">#455a64</item>
<item name="blue_grey_800" type="color">#37474f</item>
<item name="blue_grey_900" type="color">#263238</item>
</array>
</resources>
但要小心,它可能比反射效率低。
【讨论】:
是的,这很酷,但不完全是试图得到的......我试图从(例如)100 系列中获得一种颜色。 See my answer here【参考方案3】:使用反射可以检索所有 java.awt.Color 常量,大约 148 种颜色的名称
for (Field field : Color.class.getDeclaredFields())
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
&& Modifier.isFinal(modifiers) && field.getType() == Color.class)
String colorName = field.getName().toLowerCase(Locale.US);
Color color = Color.valueOf(colorName);
System.out.printf("- %s -> %s%n", colorName, color);
这使用反射,并假定常量名称是人类可读的名称。
[139] thistle -> 0xd8bfd8ff
[140] tomato -> 0xff6347ff
[141] turquoise -> 0x40e0d0ff
[142] violet -> 0xee82eeff
[143] wheat -> 0xf5deb3ff
[144] white -> 0xffffffff
[145] whitesmoke -> 0xf5f5f5ff
[146] yellow -> 0xffff00ff
[147] yellowgreen -> 0x9acd32ff
所以将颜色添加到一个列表中,然后随机选择带有random.nextInt(colors.size())
的颜色。
【讨论】:
好主意,但我不想手动创建列表然后选择。如果 Google 使用算法创建此表,我想使用它。【参考方案4】:正如Tibox 所述:
1.将材质颜色添加到array.xml
:
<array name="mdcolor_50">
<item name="red_50" type="color">#fde0dc</item>
<item name="pink_50" type="color">#fce4ec</item>
<item name="purple_50" type="color">#f3e5f5</item>
<item name="deep_purple_50" type="color">#ede7f6</item>
<item name="indigo_50" type="color">#e8eaf6</item>
<item name="blue_50" type="color">#e7e9fd</item>
<item name="light_blue_50" type="color">#e1f5fe</item>
<item name="cyan_50" type="color">#e0f7fa</item>
<item name="teal_50" type="color">#e0f2f1</item>
<item name="green_50" type="color">#d0f8ce</item>
<item name="light_green_50" type="color">#f1f8e9</item>
<item name="lime_50" type="color">#f9fbe7</item>
<item name="yellow_50" type="color">#fffde7</item>
<item name="amber_50" type="color">#fff8e1</item>
<item name="orange_50" type="color">#fff3e0</item>
<item name="deep_orange_50" type="color">#fbe9e7</item>
<item name="brown_50" type="color">#efebe9</item>
<item name="grey_50" type="color">#fafafa</item>
<item name="blue_grey_50" type="color">#eceff1</item>
</array>
<array name="mdcolor_100">
<item name="red_100" type="color">#f9bdbb</item>
<item name="pink_100" type="color">#f8bbd0</item>
<item name="purple_100" type="color">#e1bee7</item>
<item name="deep_purple_100" type="color">#d1c4e9</item>
<item name="indigo_100" type="color">#c5cae9</item>
<item name="blue_100" type="color">#d0d9ff</item>
<item name="light_blue_100" type="color">#b3e5fc</item>
<item name="cyan_100" type="color">#b2ebf2</item>
<item name="teal_100" type="color">#b2dfdb</item>
<item name="green_100" type="color">#a3e9a4</item>
<item name="light_green_100" type="color">#dcedc8</item>
<item name="lime_100" type="color">#f0f4c3</item>
<item name="yellow_100" type="color">#fff9c4</item>
<item name="amber_100" type="color">#ffecb3</item>
<item name="orange_100" type="color">#ffe0b2</item>
<item name="deep_orange_100" type="color">#ffccbc</item>
<item name="brown_100" type="color">#d7ccc8</item>
<item name="grey_100" type="color">#f5f5f5</item>
<item name="blue_grey_100" type="color">#cfd8dc</item>
</array>
<array name="mdcolor_200">
<item name="red_200" type="color">#f69988</item>
<item name="pink_200" type="color">#f48fb1</item>
<item name="purple_200" type="color">#ce93d8</item>
<item name="deep_purple_200" type="color">#b39ddb</item>
<item name="indigo_200" type="color">#9fa8da</item>
<item name="blue_200" type="color">#afbfff</item>
<item name="light_blue_200" type="color">#81d4fa</item>
<item name="cyan_200" type="color">#80deea</item>
<item name="teal_200" type="color">#80cbc4</item>
<item name="green_200" type="color">#72d572</item>
<item name="light_green_200" type="color">#c5e1a5</item>
<item name="lime_200" type="color">#e6ee9c</item>
<item name="yellow_200" type="color">#fff59d</item>
<item name="orange_200" type="color">#ffcc80</item>
<item name="deep_orange_200" type="color">#ffab91</item>
<item name="brown_200" type="color">#bcaaa4</item>
<item name="grey_200" type="color">#eeeeee</item>
<item name="blue_grey_200" type="color">#b0bec5</item>
</array>
<array name="mdcolor_300">
<item name="red_300" type="color">#f36c60</item>
<item name="pink_300" type="color">#f06292</item>
<item name="purple_300" type="color">#ba68c8</item>
<item name="deep_purple_300" type="color">#9575cd</item>
<item name="indigo_300" type="color">#7986cb</item>
<item name="blue_300" type="color">#91a7ff</item>
<item name="light_blue_300" type="color">#4fc3f7</item>
<item name="cyan_300" type="color">#4dd0e1</item>
<item name="teal_300" type="color">#4db6ac</item>
<item name="green_300" type="color">#42bd41</item>
<item name="light_green_300" type="color">#aed581</item>
<item name="lime_300" type="color">#dce775</item>
<item name="yellow_300" type="color">#fff176</item>
<item name="orange_300" type="color">#ffb74d</item>
<item name="deep_orange_300" type="color">#ff8a65</item>
<item name="brown_300" type="color">#a1887f</item>
<item name="grey_300" type="color">#e0e0e0</item>
<item name="blue_grey_300" type="color">#90a4ae</item>
</array>
<array name="mdcolor_400">
<item name="red_400" type="color">#e84e40</item>
<item name="pink_400" type="color">#ec407a</item>
<item name="purple_400" type="color">#ab47bc</item>
<item name="deep_purple_400" type="color">#7e57c2</item>
<item name="indigo_400" type="color">#5c6bc0</item>
<item name="blue_400" type="color">#738ffe</item>
<item name="light_blue_400" type="color">#29b6f6</item>
<item name="cyan_400" type="color">#26c6da</item>
<item name="teal_400" type="color">#26a69a</item>
<item name="green_400" type="color">#2baf2b</item>
<item name="light_green_400" type="color">#9ccc65</item>
<item name="lime_400" type="color">#d4e157</item>
<item name="yellow_400" type="color">#ffee58</item>
<item name="orange_400" type="color">#ffa726</item>
<item name="deep_orange_400" type="color">#ff7043</item>
<item name="brown_400" type="color">#8d6e63</item>
<item name="grey_400" type="color">#bdbdbd</item>
<item name="blue_grey_400" type="color">#78909c</item>
</array>
<array name="mdcolor_500">
<item name="red_500" type="color">#e51c23</item>
<item name="pink_500" type="color">#e91e63</item>
<item name="purple_500" type="color">#9c27b0</item>
<item name="deep_purple_500" type="color">#673ab7</item>
<item name="indigo_500" type="color">#3f51b5</item>
<item name="blue_500" type="color">#5677fc</item>
<item name="light_blue_500" type="color">#03a9f4</item>
<item name="cyan_500" type="color">#00bcd4</item>
<item name="teal_500" type="color">#009688</item>
<item name="green_500" type="color">#259b24</item>
<item name="light_green_500" type="color">#8bc34a</item>
<item name="lime_500" type="color">#cddc39</item>
<item name="yellow_500" type="color">#ffeb3b</item>
<item name="orange_500" type="color">#ff9800</item>
<item name="deep_orange_500" type="color">#ff5722</item>
<item name="brown_500" type="color">#795548</item>
<item name="grey_500" type="color">#9e9e9e</item>
<item name="blue_grey_500" type="color">#607d8b</item>
</array>
<array name="mdcolor_600">
<item name="red_600" type="color">#dd191d</item>
<item name="pink_600" type="color">#d81b60</item>
<item name="purple_600" type="color">#8e24aa</item>
<item name="deep_purple_600" type="color">#5e35b1</item>
<item name="indigo_600" type="color">#3949ab</item>
<item name="blue_600" type="color">#4e6cef</item>
<item name="light_blue_600" type="color">#039be5</item>
<item name="cyan_600" type="color">#00acc1</item>
<item name="teal_600" type="color">#00897b</item>
<item name="green_600" type="color">#0a8f08</item>
<item name="light_green_600" type="color">#7cb342</item>
<item name="lime_600" type="color">#c0ca33</item>
<item name="yellow_600" type="color">#fdd835</item>
<item name="orange_600" type="color">#fb8c00</item>
<item name="deep_orange_600" type="color">#f4511e</item>
<item name="brown_600" type="color">#6d4c41</item>
<item name="grey_600" type="color">#757575</item>
<item name="blue_grey_600" type="color">#546e7a</item>
</array>
<array name="mdcolor_700">
<item name="red_700" type="color">#d01716</item>
<item name="pink_700" type="color">#c2185b</item>
<item name="purple_700" type="color">#7b1fa2</item>
<item name="deep_purple_700" type="color">#512da8</item>
<item name="indigo_700" type="color">#303f9f</item>
<item name="blue_700" type="color">#455ede</item>
<item name="light_blue_700" type="color">#0288d1</item>
<item name="cyan_700" type="color">#0097a7</item>
<item name="teal_700" type="color">#00796b</item>
<item name="green_700" type="color">#0a7e07</item>
<item name="light_green_700" type="color">#689f38</item>
<item name="lime_700" type="color">#afb42b</item>
<item name="yellow_700" type="color">#fbc02d</item>
<item name="orange_700" type="color">#f57c00</item>
<item name="deep_orange_700" type="color">#e64a19</item>
<item name="brown_700" type="color">#5d4037</item>
<item name="grey_700" type="color">#616161</item>
<item name="blue_grey_700" type="color">#455a64</item>
</array>
<array name="mdcolor_800">
<item name="red_800" type="color">#c41411</item>
<item name="pink_800" type="color">#ad1457</item>
<item name="purple_800" type="color">#6a1b9a</item>
<item name="deep_purple_800" type="color">#4527a0</item>
<item name="indigo_800" type="color">#283593</item>
<item name="blue_800" type="color">#3b50ce</item>
<item name="light_blue_800" type="color">#0277bd</item>
<item name="cyan_800" type="color">#00838f</item>
<item name="teal_800" type="color">#00695c</item>
<item name="green_800" type="color">#056f00</item>
<item name="light_green_800" type="color">#558b2f</item>
<item name="lime_800" type="color">#9e9d24</item>
<item name="yellow_800" type="color">#f9a825</item>
<item name="orange_800" type="color">#ef6c00</item>
<item name="deep_orange_800" type="color">#d84315</item>
<item name="brown_800" type="color">#4e342e</item>
<item name="grey_800" type="color">#424242</item>
<item name="blue_grey_800" type="color">#37474f</item>
</array>
<array name="mdcolor_900">
<item name="red_900" type="color">#b0120a</item>
<item name="pink_900" type="color">#880e4f</item>
<item name="purple_900" type="color">#4a148c</item>
<item name="deep_purple_900" type="color">#311b92</item>
<item name="indigo_900" type="color">#1a237e</item>
<item name="blue_900" type="color">#2a36b1</item>
<item name="light_blue_900" type="color">#01579b</item>
<item name="cyan_900" type="color">#006064</item>
<item name="teal_900" type="color">#004d40</item>
<item name="green_900" type="color">#0d5302</item>
<item name="light_green_900" type="color">#33691e</item>
<item name="lime_900" type="color">#827717</item>
<item name="yellow_900" type="color">#f57f17</item>
<item name="orange_900" type="color">#e65100</item>
<item name="deep_orange_900" type="color">#bf360c</item>
<item name="brown_900" type="color">#3e2723</item>
<item name="grey_900" type="color">#212121</item>
<item name="blue_grey_900" type="color">#263238</item>
</array>
<array name="mdcolor_A100">
<item name="red_A100" type="color">#ff7997</item>
<item name="pink_A100" type="color">#ff80ab</item>
<item name="purple_A100" type="color">#ea80fc</item>
<item name="deep_purple_A100" type="color">#b388ff</item>
<item name="indigo_A100" type="color">#8c9eff</item>
<item name="blue_A100" type="color">#a6baff</item>
<item name="light_blue_A100" type="color">#80d8ff</item>
<item name="cyan_A100" type="color">#84ffff</item>
<item name="teal_A100" type="color">#a7ffeb</item>
<item name="green_A100" type="color">#a2f78d</item>
<item name="light_green_A100" type="color">#ccff90</item>
<item name="lime_A100" type="color">#f4ff81</item>
<item name="yellow_A100" type="color">#ffff8d</item>
<item name="orange_A100" type="color">#ffd180</item>
<item name="deep_orange_A100" type="color">#ff9e80</item>
</array>
<array name="mdcolor_A200">
<item name="red_A200" type="color">#ff5177</item>
<item name="pink_A200" type="color">#ff4081</item>
<item name="purple_A200" type="color">#e040fb</item>
<item name="deep_purple_A200" type="color">#7c4dff</item>
<item name="indigo_A200" type="color">#536dfe</item>
<item name="blue_A200" type="color">#6889ff</item>
<item name="light_blue_A200" type="color">#40c4ff</item>
<item name="cyan_A200" type="color">#18ffff</item>
<item name="teal_A200" type="color">#64ffda</item>
<item name="green_A200" type="color">#5af158</item>
<item name="light_green_A200" type="color">#b2ff59</item>
<item name="lime_A200" type="color">#eeff41</item>
<item name="yellow_A200" type="color">#ffff00</item>
<item name="orange_A200" type="color">#ffab40</item>
<item name="deep_orange_A200" type="color">#ff6e40</item>
</array>
<array name="mdcolor_A400">
<item name="red_A400" type="color">#ff2d6f</item>
<item name="pink_A400" type="color">#f50057</item>
<item name="purple_A400" type="color">#d500f9</item>
<item name="deep_purple_A400" type="color">#651fff</item>
<item name="indigo_A400" type="color">#3d5afe</item>
<item name="blue_A400" type="color">#4d73ff</item>
<item name="light_blue_A400" type="color">#00b0ff</item>
<item name="cyan_A400" type="color">#00e5ff</item>
<item name="teal_A400" type="color">#1de9b6</item>
<item name="green_A400" type="color">#14e715</item>
<item name="light_green_A400" type="color">#76ff03</item>
<item name="lime_A400" type="color">#c6ff00</item>
<item name="yellow_A400" type="color">#ffea00</item>
<item name="orange_A400" type="color">#ff9100</item>
<item name="deep_orange_A400" type="color">#ff3d00</item>
</array>
<array name="mdcolor_A700">
<item name="red_A700" type="color">#e00032</item>
<item name="pink_A700" type="color">#c51162</item>
<item name="purple_A700" type="color">#aa00ff</item>
<item name="deep_purple_A700" type="color">#6200ea</item>
<item name="indigo_A700" type="color">#304ffe</item>
<item name="blue_A700" type="color">#4d69ff</item>
<item name="light_blue_A700" type="color">#0091ea</item>
<item name="cyan_A700" type="color">#00b8d4</item>
<item name="teal_A700" type="color">#00bfa5</item>
<item name="green_A700" type="color">#12c700</item>
<item name="light_green_A700" type="color">#64dd17</item>
<item name="lime_A700" type="color">#aeea00</item>
<item name="yellow_A700" type="color">#ffd600</item>
<item name="orange_A700" type="color">#ff6d00</item>
<item name="deep_orange_A700" type="color">#dd2c00</item>
</array>
<array name="extra">
<item name="grey_1000b" type="color">#000000</item>
<item name="grey_1000w" type="color">#ffffff</item>
</array>
2。将getMDcolor()
添加到您的班级:
private int getMDcolor(String mdcolor)
int returnColor = 0;
String[] mdcolors = new String[]
"mdcolor_50",
"mdcolor_100",
"mdcolor_200",
"mdcolor_300",
"mdcolor_400",
"mdcolor_500",
"mdcolor_600",
"mdcolor_700",
"mdcolor_800",
"mdcolor_900",
"mdcolor_A100",
"mdcolor_A200",
"mdcolor_A400",
"mdcolor_A700",
"extras";
if (Arrays.asList(mdcolors).contains(mdcolor))
TypedArray colors = null;
switch (mdcolor)
case "mdcolor_50":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_50);
break;
case "mdcolors_100":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_100);
break;
case "mdcolor_200":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_200);
break;
case "mdcolor_300":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_300);
break;
case "mdcolor_400":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_400);
break;
case "mdcolor_500":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_500);
break;
case "mdcolor_600":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_600);
break;
case "mdcolor_700":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_700);
break;
case "mdcolor_800":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_800);
break;
case "mdcolor_900":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_900);
break;
case "mdcolor_A100":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_A100);
break;
case "mdcolor_A200":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_A200);
break;
case "mdcolor_A400":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_A400);
break;
case "mdcolor_A700":
colors = mContext.getResources().obtainTypedArray(R.array.mdcolor_A700);
break;
case "extra":
colors = mContext.getResources().obtainTypedArray(R.array.extra);
break;
if (colors != null)
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.BLACK);
assert colors != null;
colors.recycle();
return returnColor;
3。用法:
通过以下方式获取颜色:getMDcolor("<md color id>")
可用颜色:mdcolor_50
mdcolor_100
mdcolor_200
mdcolor_300
mdcolor_400
mdcolor_500
mdcolor_600
mdcolor_700
mdcolor_800
mdcolor_900
mdcolor_A100
mdcolor_A200
mdcolor_A400
mdcolor_A700
extras
【讨论】:
【参考方案5】:这是我写的一个类。您可以创建自己的材质调色板或获取随机颜色。
使用示例:
// get a random color.
int randomColor = MaterialColorPalette.getRandomColor("100");
// create your own material color palette
MaterialColorPalette myCustomPalette = new MaterialColorPalette(Color.CYAN);
int my200Color = myCustomPalette.getColor("200");
MaterialColorPalette.java
import android.graphics.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class MaterialColorPalette
public static final int RED_500 = 0xFFF44336;
public static final int PINK_500 = 0xFFE91E63;
public static final int PURPLE_500 = 0xFF9C27B0;
public static final int DEEP_PURPLE_500 = 0xFF673AB7;
public static final int INDIGO_500 = 0xFF3F51B5;
public static final int BLUE_500 = 0xFF2196F3;
public static final int LIGHT_BLUE_500 = 0xFF03A9F4;
public static final int CYAN_500 = 0xFF00BCD4;
public static final int TEAL_500 = 0xFF009688;
public static final int GREEN_500 = 0xFF4CAF50;
public static final int LIGHT_GREEN_500 = 0xFF8BC34A;
public static final int LIME_500 = 0xFFCDDC39;
public static final int YELLOW_500 = 0xFFFFEB3B;
public static final int AMBER_500 = 0xFFFFC107;
public static final int ORANGE_500 = 0xFFFF9800;
public static final int DEEP_ORANGE_500 = 0xFFFF5722;
public static final int BROWN_500 = 0xFF795548;
public static final int GREY_500 = 0xFF9E9E9E;
public static final int BLUE_GREY_500 = 0xFF607D8B;
private static final List<MaterialColorPalette> MATERIAL_PALETTES;
private static final Random RANDOM = new Random();
static
MATERIAL_PALETTES = new ArrayList<>();
MATERIAL_PALETTES.add(new MaterialColorPalette(RED_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(PINK_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(PURPLE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(DEEP_PURPLE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(INDIGO_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BLUE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIGHT_BLUE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(CYAN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(TEAL_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(GREEN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIGHT_GREEN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(LIME_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(YELLOW_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(AMBER_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(ORANGE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(DEEP_ORANGE_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BROWN_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(GREY_500));
MATERIAL_PALETTES.add(new MaterialColorPalette(BLUE_GREY_500));
public static int getRandomColor(String key)
return MATERIAL_PALETTES.get(RANDOM.nextInt(MATERIAL_PALETTES.size())).getColor(key);
/**
* Lighten or darken a color
*
* @param color
* color value
* @param percent
* -1.0 to 1.0
* @return new shaded color
* @see #shadeColor(String, double)
*/
public static int shadeColor(int color, double percent)
return shadeColor(String.format("#%06X", (0xFFFFFF & color)), percent); // ignores alpha channel
/**
* Lighten or darken a color
*
* @param color
* 7 character string representing the color.
* @param percent
* -1.0 to 1.0
* @return new shaded color
* @see #shadeColor(int, double)
*/
public static int shadeColor(String color, double percent)
// based off http://***.com/a/13542669/1048340
long f = Long.parseLong(color.substring(1), 16);
double t = percent < 0 ? 0 : 255;
double p = percent < 0 ? percent * -1 : percent;
long R = f >> 16;
long G = f >> 8 & 0x00FF;
long B = f & 0x0000FF;
int red = (int) (Math.round((t - R) * p) + R);
int green = (int) (Math.round((t - G) * p) + G);
int blue = (int) (Math.round((t - B) * p) + B);
return Color.rgb(red, green, blue);
private final HashMap<String, Integer> palette = new HashMap<>();
/**
* @param primary
* the 500 color
*/
public MaterialColorPalette(int primary)
palette.put("50", shadeColor(primary, 0.9));
palette.put("100", shadeColor(primary, 0.7));
palette.put("200", shadeColor(primary, 0.5));
palette.put("300", shadeColor(primary, 0.333));
palette.put("400", shadeColor(primary, 0.166));
palette.put("500", primary);
palette.put("600", shadeColor(primary, -0.125));
palette.put("700", shadeColor(primary, -0.25));
palette.put("800", shadeColor(primary, -0.375));
palette.put("900", shadeColor(primary, -0.5));
palette.put("A100", shadeColor(primary, 0.7));
palette.put("A200", shadeColor(primary, 0.5));
palette.put("A400", shadeColor(primary, 0.166));
palette.put("A700", shadeColor(primary, -0.25));
public int getColor(String key)
return palette.get(key);
public void putColor(String key, int color)
palette.put(key, color);
只需将类复制到您的项目中并添加包声明即可。
【讨论】:
以上是关于如何随机获得 Material Design 颜色?的主要内容,如果未能解决你的问题,请参考以下文章
设置 SVG Material Design Icons 的背景颜色
如何在 Android 中实际使用 Material Design 工具生成的调色板?
有 Flutter 的 Material Design 颜色图吗?