【Android】getColor(int id)在API23时过时
来源:互联网 发布:nginx 防御ddos 编辑:程序博客网 时间:2024/06/06 12:57
1.getColor(int id)过时
最近发现getColor(int id)在API版本23时(Android 6.0)已然过时,以下为getColor(int id)源码(Resource.java):
/** * Returns a color integer associated with a particular resource ID. If the * resource holds a complex {@link ColorStateList}, then the default color * from the set is returned. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * * @throws NotFoundException Throws NotFoundException if the given ID does * not exist. * * @return A single color value in the form 0xAARRGGBB. * @deprecated Use {@link #getColor(int, Theme)} instead. */ @ColorInt @Deprecated public int getColor(@ColorRes int id) throws NotFoundException { return getColor(id, null); }
此类中使用了新方法getColor(@ColorRes int id, @Nullable Theme theme)来获取资源:
/** * Returns a themed color integer associated with a particular resource ID. * If the resource holds a complex {@link ColorStateList}, then the default * color from the set is returned. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @param theme The theme used to style the color attributes, may be * {@code null}. * * @throws NotFoundException Throws NotFoundException if the given ID does * not exist. * * @return A single color value in the form 0xAARRGGBB. */ @ColorInt public int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException { TypedValue value; synchronized (mAccessLock) { value = mTmpValue; if (value == null) { value = new TypedValue(); } getValue(id, value, true); if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) { mTmpValue = value; return value.data; } else if (value.type != TypedValue.TYPE_STRING) { throw new NotFoundException( "Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); } mTmpValue = null; } final ColorStateList csl = loadColorStateList(value, id, theme); synchronized (mAccessLock) { if (mTmpValue == null) { mTmpValue = value; } } return csl.getDefaultColor(); }
2.当前替代的使用方法
// 同时兼容高、低版本ContextCompat.getColor(Context context, int id);
1.源码解析
ContextCompat.getColor(Context context, int id)源码如下:
/** * Returns a color associated with a particular resource ID * <p> * Starting in {@link android.os.Build.VERSION_CODES#M}, the returned * color will be styled for the specified Context's theme. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @return A single color value in the form 0xAARRGGBB. * @throws android.content.res.Resources.NotFoundException if the given ID * does not exist. */ public static final int getColor(Context context, int id) { final int version = Build.VERSION.SDK_INT; if (version >= 23) { return ContextCompatApi23.getColor(context, id); } else { return context.getResources().getColor(id); } }
从这里我们可以看出,当API版本>=23时,使用ContextCompatApi23.getColor(context, id)方法,当API版本<23时,使用context.getResources().getColor(id)方法获取相应色值,继续往下看:
ContextCompatApi23.getColor(context, id)源码如下:
// ContextCompatApi23.javapublic static int getColor(Context context, int id) { return context.getColor(id); } // 继续解析context.getColor(id)源码--Context.java: /** * Returns a color associated with a particular resource ID and styled for * the current theme. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @return A single color value in the form 0xAARRGGBB. * @throws android.content.res.Resources.NotFoundException if the given ID * does not exist. */ @Nullable public final int getColor(int id) { // 注:这里调用了Resource.java类中的getColor(int id, Theme theme)方法 return getResources().getColor(id, getTheme()); }
context.getResources().getColor(id)源码如下:
/** * Returns a color integer associated with a particular resource ID. If the * resource holds a complex {@link ColorStateList}, then the default color * from the set is returned. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * * @throws NotFoundException Throws NotFoundException if the given ID does * not exist. * * @return A single color value in the form 0xAARRGGBB. * @deprecated Use {@link #getColor(int, Theme)} instead. */ @ColorInt @Deprecated public int getColor(@ColorRes int id) throws NotFoundException { // 这里同样调用了Resource.java中的getColor(int id, Theme theme)方法,传入的Theme值为null(低版本)--等同于调用getColor(int id)--参考上面源码. return getColor(id, null); }
7 2
- getColor(int id)在API23时过时
- 【Android】getColor(int id)在API23时过时
- getColor(int id)在API23过时问题
- getDrawable(int id) getColor(int id) is deprecate 已过时
- Android getResource().getColor(int)过时解决方法
- 'getColor(int)' is deprecated ,getColor过时
- Android getColor方法提示过时
- getColor(int id) is deprecated
- getColor方法在6.0中已经过时
- android-6.0-23的getColor过时
- android-6.0-23的getColor过时
- Android 6.0 的getColor()方法过时
- Android getResources().getColor() 过时 替代方法
- Android getResources().getColor() 过时 替代方法
- AndroidStudio getColor方法过时
- getColor过时方法替代
- Android-6.0-23的getColor()过时替代方法
- Android开发中过时方法总结getColor,singeLine
- ios 判断类型
- ueditor使用会于第三方发生冲突
- 华为oj:计算字符串最后一个单词的长度,单词用空格隔开
- SVN使用教程总结
- MEANjs Angularjs NgTable Mongo
- 【Android】getColor(int id)在API23时过时
- rot90--矩阵旋转
- Android写文件到SDCard的一般过程和代码
- 数据挖掘day1——数据规约之特征规约
- oracle自治事务
- H.264视频流的传输与载荷
- fliplr--矩阵左右翻转
- 安卓图片缩放
- 推荐Pixel Winch和LICEcap