Search in sources :

Example 81 with ColorStateList

use of android.content.res.ColorStateList in project MagicaSakura by Bilibili.

the class ColorStateListUtils method inflateColorStateList.

static ColorStateList inflateColorStateList(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    LinkedList<int[]> stateList = new LinkedList<>();
    LinkedList<Integer> colorList = new LinkedList<>();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        TypedArray a1 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.color });
        final int value = a1.getResourceId(0, Color.MAGENTA);
        final int baseColor = value == Color.MAGENTA ? Color.MAGENTA : ThemeUtils.replaceColorById(context, value);
        a1.recycle();
        TypedArray a2 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.alpha });
        final float alphaMod = a2.getFloat(0, 1.0f);
        a2.recycle();
        colorList.add(alphaMod != 1.0f ? ColorUtils.setAlphaComponent(baseColor, Math.round(Color.alpha(baseColor) * alphaMod)) : baseColor);
        stateList.add(extractStateSet(attrs));
    }
    if (stateList.size() > 0 && stateList.size() == colorList.size()) {
        int[] colors = new int[colorList.size()];
        for (int i = 0; i < colorList.size(); i++) {
            colors[i] = colorList.get(i);
        }
        return new ColorStateList(stateList.toArray(new int[stateList.size()][]), colors);
    }
    return null;
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) LinkedList(java.util.LinkedList)

Example 82 with ColorStateList

use of android.content.res.ColorStateList in project Douya by DreaminginCodeZH.

the class NavigationAccountListLayout method init.

private void init() {
    ViewUtils.inflateInto(R.layout.navigation_account_list_layout, this);
    ButterKnife.bind(this);
    final Context context = getContext();
    ColorStateList iconTintList = ViewUtils.getColorStateListFromAttrRes(android.R.attr.textColorSecondary, context);
    for (TextView menuItem : mMenuItems) {
        Drawable icon = menuItem.getCompoundDrawables()[0];
        icon = TintHelper.tintDrawable(icon, iconTintList);
        TextViewCompat.setCompoundDrawablesRelative(menuItem, icon, null, null, null);
    }
    mAddAccountItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            AccountUtils.addAccount(AppUtils.getActivityFromContext(context));
        }
    });
    mRemoveCurrentAccountItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onRemoveCurrentAccount();
            }
        }
    });
    mManageAccountsItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            AppUtils.startActivity(IntentUtils.makeSyncSettings(), context);
        }
    });
}
Also used : Context(android.content.Context) Drawable(android.graphics.drawable.Drawable) ColorStateList(android.content.res.ColorStateList) TextView(android.widget.TextView) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView)

Example 83 with ColorStateList

use of android.content.res.ColorStateList in project Android-Bootstrap by Bearded-Hen.

the class BootstrapDrawableFactory method bootstrapButtonText.

/**
     * Generates a colorstatelist for a bootstrap button
     *
     * @param context the current context
     * @param outline whether the button is outlined
     * @param brand   the button brand
     * @return the color state list
     */
static ColorStateList bootstrapButtonText(Context context, boolean outline, BootstrapBrand brand) {
    int defaultColor = outline ? brand.defaultFill(context) : brand.defaultTextColor(context);
    int activeColor = outline ? ColorUtils.resolveColor(android.R.color.white, context) : brand.activeTextColor(context);
    int disabledColor = outline ? brand.disabledFill(context) : brand.disabledTextColor(context);
    if (outline && brand instanceof DefaultBootstrapBrand) {
        // special case
        DefaultBootstrapBrand db = (DefaultBootstrapBrand) brand;
        if (db == DefaultBootstrapBrand.SECONDARY) {
            defaultColor = ColorUtils.resolveColor(R.color.bootstrap_brand_secondary_border, context);
            disabledColor = defaultColor;
        }
    }
    return new ColorStateList(getStateList(), getColorList(defaultColor, activeColor, disabledColor));
}
Also used : DefaultBootstrapBrand(com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand) ColorStateList(android.content.res.ColorStateList) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 84 with ColorStateList

use of android.content.res.ColorStateList in project AndroidChromium by JackyAndroid.

the class LocationBarLayout method getColorStateList.

/**
     * @param securityLevel The security level for which the color will be returned.
     * @param provider The {@link ToolbarDataProvider}.
     * @param resources The Resources for the Context.
     * @param isOmniboxOpaque Whether the omnibox is an opaque color.
     * @return The {@link ColorStateList} to use to tint the security state icon.
     */
public static ColorStateList getColorStateList(int securityLevel, ToolbarDataProvider provider, Resources resources, boolean isOmniboxOpaque) {
    ColorStateList list = null;
    int color = provider.getPrimaryColor();
    boolean needLightIcon = ColorUtils.shouldUseLightForegroundOnBackground(color);
    if (provider.isIncognito() || needLightIcon) {
        // For a dark theme color, use light icons.
        list = ApiCompatibilityUtils.getColorStateList(resources, R.color.light_mode_tint);
    } else if (!ColorUtils.isUsingDefaultToolbarColor(resources, color) && !isOmniboxOpaque) {
        // For theme colors which are not dark and are also not
        // light enough to warrant an opaque URL bar, use dark
        // icons.
        list = ApiCompatibilityUtils.getColorStateList(resources, R.color.dark_mode_tint);
    } else {
        // For the default toolbar color, use a green or red icon.
        if (securityLevel == ConnectionSecurityLevel.DANGEROUS) {
            list = ApiCompatibilityUtils.getColorStateList(resources, R.color.google_red_700);
        } else if (securityLevel == ConnectionSecurityLevel.SECURE || securityLevel == ConnectionSecurityLevel.EV_SECURE) {
            list = ApiCompatibilityUtils.getColorStateList(resources, R.color.google_green_700);
        } else {
            list = ApiCompatibilityUtils.getColorStateList(resources, R.color.dark_mode_tint);
        }
    }
    assert list != null : "Missing ColorStateList for Security Button.";
    return list;
}
Also used : ColorStateList(android.content.res.ColorStateList)

Example 85 with ColorStateList

use of android.content.res.ColorStateList in project AndroidChromium by JackyAndroid.

the class ToolbarPhone method updateVisualsForToolbarState.

private void updateVisualsForToolbarState() {
    final boolean isIncognito = isIncognito();
    // These are important for setting visual state while the entering or leaving the tab
    // switcher.
    boolean inOrEnteringStaticTab = mTabSwitcherState == STATIC_TAB || mTabSwitcherState == EXITING_TAB_SWITCHER;
    boolean inOrEnteringTabSwitcher = !inOrEnteringStaticTab;
    VisualState newVisualState = computeVisualState(inOrEnteringTabSwitcher);
    // do not match then cancel the animation below.
    if (mBrandColorTransitionActive && isVisualStateValidForBrandColorTransition(mVisualState) && isVisualStateValidForBrandColorTransition(newVisualState)) {
        return;
    } else if (mBrandColorTransitionAnimation != null && mBrandColorTransitionAnimation.isRunning()) {
        mBrandColorTransitionAnimation.cancel();
    }
    boolean visualStateChanged = mVisualState != newVisualState;
    int currentPrimaryColor = getToolbarDataProvider().getPrimaryColor();
    int themeColorForProgressBar = currentPrimaryColor;
    // If The page is native force the use of the standard theme for the progress bar.
    if (getToolbarDataProvider() != null && getToolbarDataProvider().getTab() != null && getToolbarDataProvider().getTab().isNativePage()) {
        VisualState visualState = isIncognito() ? VisualState.INCOGNITO : VisualState.NORMAL;
        themeColorForProgressBar = getToolbarColorForVisualState(visualState);
    }
    if (mVisualState == VisualState.BRAND_COLOR && !visualStateChanged) {
        boolean useLightToolbarDrawables = ColorUtils.shouldUseLightForegroundOnBackground(currentPrimaryColor);
        boolean unfocusedLocationBarUsesTransparentBg = !ColorUtils.shouldUseOpaqueTextboxBackground(currentPrimaryColor);
        if (useLightToolbarDrawables != mUseLightToolbarDrawables || unfocusedLocationBarUsesTransparentBg != mUnfocusedLocationBarUsesTransparentBg) {
            visualStateChanged = true;
        } else {
            updateToolbarBackground(VisualState.BRAND_COLOR);
            getProgressBar().setThemeColor(themeColorForProgressBar, isIncognito());
        }
    }
    mVisualState = newVisualState;
    updateOverlayDrawables();
    updateShadowVisibility();
    updateUrlExpansionAnimation();
    if (!visualStateChanged) {
        if (mVisualState == VisualState.NEW_TAB_NORMAL) {
            updateNtpTransitionAnimation();
        } else {
            resetNtpAnimationValues();
        }
        return;
    }
    mUseLightToolbarDrawables = false;
    mUnfocusedLocationBarUsesTransparentBg = false;
    mLocationBarBackgroundAlpha = 255;
    updateToolbarBackground(mVisualState);
    getProgressBar().setThemeColor(themeColorForProgressBar, isIncognito());
    if (inOrEnteringTabSwitcher) {
        mUseLightToolbarDrawables = true;
        mLocationBarBackgroundAlpha = LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA;
        getProgressBar().setBackgroundColor(mProgressBackBackgroundColorWhite);
        getProgressBar().setForegroundColor(ApiCompatibilityUtils.getColor(getResources(), R.color.progress_bar_foreground_white));
    } else if (isIncognito()) {
        mUseLightToolbarDrawables = true;
        mLocationBarBackgroundAlpha = LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA;
    } else if (mVisualState == VisualState.BRAND_COLOR) {
        mUseLightToolbarDrawables = ColorUtils.shouldUseLightForegroundOnBackground(currentPrimaryColor);
        mUnfocusedLocationBarUsesTransparentBg = !ColorUtils.shouldUseOpaqueTextboxBackground(currentPrimaryColor);
        mLocationBarBackgroundAlpha = mUnfocusedLocationBarUsesTransparentBg ? LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA : 255;
    }
    if (mToggleTabStackButton != null) {
        mToggleTabStackButton.setImageDrawable(mUseLightToolbarDrawables ? mTabSwitcherButtonDrawableLight : mTabSwitcherButtonDrawable);
        if (mTabSwitcherAnimationTabStackDrawable != null) {
            mTabSwitcherAnimationTabStackDrawable.setTint(mUseLightToolbarDrawables ? mLightModeTint : mDarkModeTint);
        }
    }
    mMenuButton.setTint(mUseLightToolbarDrawables ? mLightModeTint : mDarkModeTint);
    if (mShowMenuBadge && inOrEnteringStaticTab) {
        setAppMenuUpdateBadgeDrawable(mUseLightToolbarDrawables);
    }
    ColorStateList tint = mUseLightToolbarDrawables ? mLightModeTint : mDarkModeTint;
    if (mIsHomeButtonEnabled)
        mHomeButton.setTint(tint);
    mLocationBar.updateVisualsForState();
    // background of the location bar.
    if (isIncognito) {
        mLocationBar.setPadding(0, mLocationBarBackgroundPadding.top, 0, mLocationBarBackgroundPadding.bottom);
    } else {
        mLocationBar.setPadding(mLocationBarBackgroundPadding.left, mLocationBarBackgroundPadding.top, mLocationBarBackgroundPadding.right, mLocationBarBackgroundPadding.bottom);
    }
    // its value when entering and exiting TabSwitcher mode.
    if (isLocationBarShownInNTP() && inOrEnteringStaticTab) {
        updateNtpTransitionAnimation();
    }
    mNewTabButton.setIsIncognito(isIncognito);
    CharSequence newTabContentDescription = getResources().getText(isIncognito ? R.string.accessibility_toolbar_btn_new_incognito_tab : R.string.accessibility_toolbar_btn_new_tab);
    if (mNewTabButton != null && !newTabContentDescription.equals(mNewTabButton.getContentDescription())) {
        mNewTabButton.setContentDescription(newTabContentDescription);
    }
    getMenuButtonWrapper().setVisibility(View.VISIBLE);
}
Also used : ColorStateList(android.content.res.ColorStateList) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Aggregations

ColorStateList (android.content.res.ColorStateList)280 Paint (android.graphics.Paint)90 TypedArray (android.content.res.TypedArray)76 Drawable (android.graphics.drawable.Drawable)48 TextPaint (android.text.TextPaint)42 Resources (android.content.res.Resources)25 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)22 TypedValue (android.util.TypedValue)21 TextView (android.widget.TextView)19 Context (android.content.Context)18 SuppressLint (android.annotation.SuppressLint)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 AllCapsTransformationMethod (android.text.method.AllCapsTransformationMethod)13 RippleDrawable (android.graphics.drawable.RippleDrawable)12 StateListDrawable (android.graphics.drawable.StateListDrawable)12 View (android.view.View)12 Nullable (android.annotation.Nullable)10 Bitmap (android.graphics.Bitmap)10 TextAppearanceSpan (android.text.style.TextAppearanceSpan)9 PorterDuff (android.graphics.PorterDuff)8