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);    }

更多相关文章

  1. 收藏android源码项目
  2. android 一个应用去获取另一个应用assets下面的资源通过框架代码
  3. MTK Android(安卓)如何自动挂断电话
  4. Android(安卓)实现GIF播放(解码)
  5. Android(安卓)5.0 API变化
  6. android 退出全部activity的方法
  7. getReadableDatabase() 和 getWritableDatabase()
  8. Android中Log机制
  9. android ListView本行控件操作本行其它控件的重要方法(绝对原创,本

随机推荐

  1. android中的apk签名
  2. Android经典应用程序开发
  3. Android(安卓)OOM内存溢出解决方案之一
  4. Android(安卓)的开源电话/通讯/IM聊天项
  5. android service 学习
  6. 安卓 Android之开发简单小应用(一)
  7. Android开发周刊 第四期
  8. 用kotlin打印出漂亮的android日志(三)—
  9. Android开发从入门到精通(2)
  10. unity3d + android 好东西。。。