【Android】getColor(int id)在API23时过时

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
     * 

* 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.java
public 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);
    }

你可能感兴趣的:(【Android】基础技能)