Fill ArrayList with colors for Android

后端 未结 5 601
夕颜
夕颜 2021-01-21 01:52

I want to create 2 ArrayList. One holding 16 colors, the other one holding 139.

I have the list with colors (both RGB as 255,126,32 and Hex as 0xFFFF2552). I want to use

5条回答
  •  悲哀的现实
    2021-01-21 02:01

    You could try:

    int[] colors = new int[] {Color.rgb(1,1,1), Color.rgb(...)};
    

    For example, but I don't think it's a good idea to decide only using "one line" argument.

    List coloras = Arrays.asList(new Integer[]{Color.rgb(1, 1, 1), Color.rgb(...)});
    

    Will also work.

    You can create an arraylist in arrays.xml file:

    
                
            #ff0000
            #00ff00  
            #0000ff
        
    
    

    Then use the loop to read them:

    String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.colors);
    List colors = new ArrayList();
    for (int i = 0; i < colorsTxt.length; i++) {
        int newColor = Color.parseColor(colorsTxt[i]);
        colors.add(newColor);
    }
    

    In my opinion keeping colors in the list is the most convinient solution.

    To take a color from the list randomly, you do:

    int rand = new Random().nextInt(colors.size());
    Integer color = colors.get(rand);
    

提交回复
热议问题