如何在Android studio中随机生成一个字符串并随机着色该字符串

问题描述 投票:-10回答:1

我正在尝试让一个程序随机生成一个文本,并让该文本也随机变成颜色,但我不知道如何。可以是Java或Kotlin。

java android random text kotlin
1个回答
1
投票

有两种方法

  1. 使用预定义的颜色as in 在colors.xml中 <item name="blue" type="color">#FF33B5E5</item> <item name="purple" type="color">#FFAA66CC</item> <item name="green" type="color">#FF99CC00</item> <item name="orange" type="color">#FFFFBB33</item> <item name="red" type="color">#FFFF4444</item> <item name="darkblue" type="color">#FF0099CC</item> <item name="darkpurple" type="color">#FF9933CC</item> <item name="darkgreen" type="color">#FF669900</item> <item name="darkorange" type="color">#FFFF8800</item> <item name="darkred" type="color">#FFCC0000</item> <integer-array name="androidcolors"> <item>@color/blue</item> <item>@color/purple</item> <item>@color/green</item> <item>@color/orange</item> <item>@color/red</item> <item>@color/darkblue</item> <item>@color/darkpurple</item> <item>@color/darkgreen</item> <item>@color/darkorange</item> <item>@color/darkred</item> </integer-array> 在onCreate() int[] androidColors = getResources().getIntArray(R.array.androidcolors); int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)]; view.setBackgroundColor(randomAndroidColor);
  2. 使用随机RGB着色as in Random rnd = new Random(); currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
© www.soinside.com 2019 - 2024. All rights reserved.