Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用一些键在android中创建随机颜色(对于同一个键,它应该生成相同的颜色)_Java_Android_Colors - Fatal编程技术网

Java 用一些键在android中创建随机颜色(对于同一个键,它应该生成相同的颜色)

Java 用一些键在android中创建随机颜色(对于同一个键,它应该生成相同的颜色),java,android,colors,Java,Android,Colors,我正在开发一个聊天应用程序,为此,我需要为用户配置文件图片生成随机颜色。但我需要为同一个人生成相同的颜色。我有一个唯一的用户id。我不想在生成颜色代码一次后保存颜色代码。所以,基本上我需要一种方法来获得颜色代码 int getUserColourCode(String userId) { //code needed. //It should return random colour code (int) with respect to user id. //I would li

我正在开发一个聊天应用程序,为此,我需要为用户配置文件图片生成随机颜色。但我需要为同一个人生成相同的颜色。我有一个唯一的用户id。我不想在生成颜色代码一次后保存颜色代码。所以,基本上我需要一种方法来获得颜色代码

int getUserColourCode(String userId) {
   //code needed.
   //It should return random colour code (int) with respect to user id.
   //I would like to exclude light shade colours (dark and semi dark colours are preferred).
}

提前感谢。

首先,您需要传递整数作为函数的参数。请使用
int userId
,而不是
String userId
。如果仍要将字符串类型作为参数传递,则需要将其解析为整数

其次,需要定义数组中所需的所有颜色

基本上,您不能使用Java随机数内置函数,因为它将始终生成新的随机数,因此它将不符合您的需要

int getUserColourCode(String userId) {
    int id = Integer.parseInt(userId);

    //create integer color as much as you want,
    int[] colors = {Color.BLUE, Color.CYAN, Color.MAGENTA, Color.parseColor("#ff00f8")};

    int colorLength = colors.length - 1;
    int randomNumber = id % colorLength;
    return colors[randomNumber];
}
如果将integer作为参数的类型传递,则可以使用:

int getUserColourCode(int userId) {

    //create integer color as much as you want,
    int[] colors = {Color.BLUE, Color.CYAN, Color.MAGENTA, Color.parseColor("#ff00f8")};

    int colorLength = colors.length - 1;
    int randomNumber = userId % colorLength;
    return colors[randomNumber];
}

使用此选项,您可以从颜色代码生成随机颜色, 但是,如果需要从用户处获取颜色,则需要本地数据库的共享首选项值来保存用户的颜色

int getUserColourCode(String userId) {
  Random rnd = new Random(); 
   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));      
 return color;
}
int GetUserColor代码(字符串用户ID){ StringBuilder input1=新的StringBuilder()

input1.append(userId);
input1=input1.reverse();
字符串对[]={“0”、“0”、“0”、“0”、“0”、“0”、“0”、“0”、“0”};
char[]character=input1.toString().toCharray();

对于(int i=0;i)您只需要一种方法将字符串转换为3(或4)个字节。例如,您可以获取字符串的字节数,并获取字符串的最后3个字节。这是否回答了您的问题?谢谢,此方法为我提供了一个精确的解决方案。
input1.append(userId);
input1=input1.reverse();
String pair[]={"0","0","0","0","0","0","0","0"};
char[] character = input1.toString().toCharArray();

for(int i=0;i<character.length;i++)
{
  pair[i]=String.valueOf(character[i]);
}
int color = Color.argb((Integer.parseInt(pair[0]+pair[1])*2)+50, (Integer.parseInt(pair[2]+pair[3])*2)+50, (Integer.parseInt(pair[4]+pair[5])*2)+50, (Integer.parseInt(pair[6]+pair[7])*2)+50);
return color;