有个需求是需要传十六进制色值到后台,因为颜色值都配置在Colors.xml,所以需要转换一下,方法如下
colors.xml文件内容@H_301_3@ <color name="style_color">#67ac66</color>@H_301_3@ @H_301_3@ @H_301_3@ 调用方法@H_301_3@ changeColor(mContext,R.color.style_color)@H_301_3@ @H_301_3@ @H_301_3@ /得到内容 67ac66@H_301_3@ /@H_301_3@ public String changeColor(Context context,int id){@H_301_3@ StringBuffer stringBuffer = new StringBuffer();@H_301_3@ int color = context.getResources().getColor(id);@H_301_3@ int red = (color & 0xff0000) >> 16;@H_301_3@ int green = (color & 0x00ff00) >> 8;@H_301_3@ int blue = (color & 0x0000ff);
stringBuffer.append(Integer.toHexString(red)); stringBuffer.append(Integer.toHexString(green)); stringBuffer.append(Integer.toHexString(blue)); LogUtils.d("changecolor="+stringBuffer.toString()); return stringBuffer.toString(); }