How can I save colors in array.xml and get its back to Color[] array

后端 未结 8 1267
借酒劲吻你
借酒劲吻你 2020-12-04 21:01

How can I save color values inside array.xml and retrieve its back to my code as Color [] array?

Thanks beforehand!

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 21:37

    Define your color resources, then add them to an array for access.

    
    
        #FF007F
        #FF0000
        #FF7F00
        #FFFF00
        #7FFF00
        #00FF00
        #00FF7F
        #00FFFF
        #007FFF
        #0000FF
        #7F00FF
        #FF00FF
    
        
            @color/bright_pink
            @color/red
            @color/orange
            @color/yellow
            @color/chartreuse
            @color/green
            @color/spring_green
            @color/cyan
            @color/azure
            @color/blue
            @color/violet
            @color/magenta
        
    
    

    Then access them like this:

    int[] rainbow = context.getResources().getIntArray(R.array.rainbow);
    
    for (int i = 0; i < tileColumns; i++) {
        paint.setColor(rainbow[i]);
        // Do something with the paint.
    }
    

提交回复
热议问题