How to set paint.setColor(R.color.white)

后端 未结 5 438
盖世英雄少女心
盖世英雄少女心 2020-12-29 23:09

I have a custom View that uses Paint and Canvas to draw objects. My question is how to set:

int color = R.color.white;
paint.setColor(color);
5条回答
  •  清歌不尽
    2020-12-29 23:34

    first get your color from xml file

    int color = context.getResources().getColor(R.color.colorPrimary); // old
    

    is deprecated now, use this instead

    int color = ContextCompat.getColor(context, R.color.colorPrimary); // new
    

    set color

    paint.setColor(color);
    

    xml file preview: res/values/color.xml

    
    
        #3F51B5
        #303F9F
        #FF4081
    
    

提交回复
热议问题