Java Code Examples for android.content.res.Resources.Theme#resolveAttributes()

The following examples show how to use android.content.res.Resources.Theme#resolveAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GradientColor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void applyRootAttrsTheme(Theme t) {
    final TypedArray a = t.resolveAttributes(mThemeAttrs, R.styleable.GradientColor);
    // mThemeAttrs will be set to null if if there are no theme attributes in the
    // typed array.
    mThemeAttrs = a.extractThemeAttrs(mThemeAttrs);
    // merging the attributes update inside the updateRootElementState().
    updateRootElementState(a);

    // Account for any configuration changes.
    mChangingConfigurations |= a.getChangingConfigurations();
    a.recycle();
}
 
Example 2
Source File: ColorStateList.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Applies a theme to this color state list.
 * <p>
 * <strong>Note:</strong> Applying a theme may affect the changing
 * configuration parameters of this color state list. After calling this
 * method, any dependent configurations must be updated by obtaining the
 * new configuration mask from {@link #getChangingConfigurations()}.
 *
 * @param t the theme to apply
 */
private void applyTheme(Theme t) {
    if (mThemeAttrs == null) {
        return;
    }

    boolean hasUnresolvedAttrs = false;

    final int[][] themeAttrsList = mThemeAttrs;
    final int N = themeAttrsList.length;
    for (int i = 0; i < N; i++) {
        if (themeAttrsList[i] != null) {
            final TypedArray a = t.resolveAttributes(themeAttrsList[i],
                    R.styleable.ColorStateListItem);

            final float defaultAlphaMod;
            if (themeAttrsList[i][R.styleable.ColorStateListItem_color] != 0) {
                // If the base color hasn't been resolved yet, the current
                // color's alpha channel is either full-opacity (if we
                // haven't resolved the alpha modulation yet) or
                // pre-modulated. Either is okay as a default value.
                defaultAlphaMod = Color.alpha(mColors[i]) / 255.0f;
            } else {
                // Otherwise, the only correct default value is 1. Even if
                // nothing is resolved during this call, we can apply this
                // multiple times without losing of information.
                defaultAlphaMod = 1.0f;
            }

            // Extract the theme attributes, if any, before attempting to
            // read from the typed array. This prevents a crash if we have
            // unresolved attrs.
            themeAttrsList[i] = a.extractThemeAttrs(themeAttrsList[i]);
            if (themeAttrsList[i] != null) {
                hasUnresolvedAttrs = true;
            }

            final int baseColor = a.getColor(
                    R.styleable.ColorStateListItem_color, mColors[i]);
            final float alphaMod = a.getFloat(
                    R.styleable.ColorStateListItem_alpha, defaultAlphaMod);
            mColors[i] = modulateColorAlpha(baseColor, alphaMod);

            // Account for any configuration changes.
            mChangingConfigurations |= a.getChangingConfigurations();

            a.recycle();
        }
    }

    if (!hasUnresolvedAttrs) {
        mThemeAttrs = null;
    }

    onColorsChanged();
}
 
Example 3
Source File: GradientColor.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Apply theme to all the items.
 */
private void applyItemsAttrsTheme(Theme t) {
    if (mItemsThemeAttrs == null) {
        return;
    }

    boolean hasUnresolvedAttrs = false;

    final int[][] themeAttrsList = mItemsThemeAttrs;
    final int N = themeAttrsList.length;
    for (int i = 0; i < N; i++) {
        if (themeAttrsList[i] != null) {
            final TypedArray a = t.resolveAttributes(themeAttrsList[i],
                    R.styleable.GradientColorItem);

            // Extract the theme attributes, if any, before attempting to
            // read from the typed array. This prevents a crash if we have
            // unresolved attrs.
            themeAttrsList[i] = a.extractThemeAttrs(themeAttrsList[i]);
            if (themeAttrsList[i] != null) {
                hasUnresolvedAttrs = true;
            }

            mItemColors[i] = a.getColor(R.styleable.GradientColorItem_color, mItemColors[i]);
            mItemOffsets[i] = a.getFloat(R.styleable.GradientColorItem_offset, mItemOffsets[i]);
            if (DBG_GRADIENT) {
                Log.v(TAG, "applyItemsAttrsTheme Colors[i] " + i + " " +
                        Integer.toHexString(mItemColors[i]));
                Log.v(TAG, "Offsets[i] " + i + " " + mItemOffsets[i]);
            }

            // Account for any configuration changes.
            mChangingConfigurations |= a.getChangingConfigurations();

            a.recycle();
        }
    }

    if (!hasUnresolvedAttrs) {
        mItemsThemeAttrs = null;
    }
}