Android: Color To Int conversion

前端 未结 5 1149
时光说笑
时光说笑 2020-12-06 03:56

This might be a stupid question but I\'m surprised that Paint class has no setColor(Color c) method. I want to do the following:

pu         


        
相关标签:
5条回答
  • 2020-12-06 04:27

    R.color.black or some color are obviously integers. It needs a RGB value. You can give your own like #FF123454 which represents various primary colors

    0 讨论(0)
  • 2020-12-06 04:29

    Any color parse into int simplest two way here:

    1) Get System Color

    int redColorValue = Color.RED;
    

    2) Any Color Hex Code as a String Argument

    int greenColorValue = Color.parseColor("#00ff00")
    

    MUST REMEMBER in above code Color class must be android.graphics...!

    0 讨论(0)
  • 2020-12-06 04:39

    All the methods and variables in Color are static. You can not instantiate a Color object.

    Official Color Docs

    The Color class defines methods for creating and converting color ints.

    Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue.

    The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components.

    The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue.

    Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution.

    Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF

    0 讨论(0)
  • 2020-12-06 04:39

    I think it should be R.color.black

    Also take a look at Converting android color string in runtime into int

    0 讨论(0)
  • 2020-12-06 04:48

    Paint DOES have set color function.

    /**
     * Set the paint's color. Note that the color is an int containing alpha
     * as well as r,g,b. This 32bit value is not premultiplied, meaning that
     * its alpha can be any value, regardless of the values of r,g,b.
     * See the Color class for more details.
     *
     * @param color The new color (including alpha) to set in the paint.
     */
    public native void setColor(@ColorInt int color);
    

    As an Android developer, I set paint color like this...

    paint.setColor(getResources().getColor(R.color.xxx));
    

    I define the color value on color.xml something like...

    <color name="xxx">#008fd2</color>
    

    By the way if you want to the hex RGB value of specific color value, then you can check website like this: http://www.rapidtables.com/web/color/RGB_Color.htm

    I hope this helps ! Enjoy coding!

    0 讨论(0)
提交回复
热议问题