Example usage for android.util TypedValue TYPE_FIRST_COLOR_INT

List of usage examples for android.util TypedValue TYPE_FIRST_COLOR_INT

Introduction

In this page you can find the example usage for android.util TypedValue TYPE_FIRST_COLOR_INT.

Prototype

int TYPE_FIRST_COLOR_INT

To view the source code for android.util TypedValue TYPE_FIRST_COLOR_INT.

Click Source Link

Document

Identifies the start of integer values that were specified as color constants (starting with '#').

Usage

From source file:Main.java

private static boolean isColor(int type) {
    return type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT;
}

From source file:com.bilibili.magicasakura.utils.ColorStateListUtils.java

static ColorStateList createColorStateList(Context context, int resId) {
    if (resId <= 0)
        return null;

    TypedValue value = new TypedValue();
    context.getResources().getValue(resId, value, true);
    ColorStateList cl = null;/* w w w  .  j  av  a  2  s  . c om*/
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        //Assume that "color/theme_color_primary" and "color/theme_color_profile" have the same color value;
        //However, "color/theme_color_primary" need to replace by themeId, "color/theme_color_profile" not.
        //If use value.data may cause "color/theme_color_profile" still been replaced by themeId
        cl = ColorStateList.valueOf(ThemeUtils.replaceColorById(context, value.resourceId));
    } else {
        final String file = value.string.toString();
        try {
            if (file.endsWith("xml")) {
                final XmlResourceParser rp = context.getResources().getAssets()
                        .openXmlResourceParser(value.assetCookie, file);
                final AttributeSet attrs = Xml.asAttributeSet(rp);
                int type;

                while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                    // Seek parser to start tag.
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }

                cl = createFromXmlInner(context, rp, attrs);
                rp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
    return cl;
}

From source file:com.andryr.guitartuner.Utils.java

public static int getAttrColor(Context context, int attrId) {
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(attrId, typedValue, true);
    if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
            && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        return typedValue.data;
    } else {/*  w w w  . ja  va  2 s. c  o  m*/
        return context.getResources().getColor(typedValue.resourceId);
    }
}

From source file:com.nttec.everychan.ui.theme.ThemeUtils.java

private static int getThemeColor(TypedValue tmp, Theme theme, int attrId, int defaultValue) {
    if (tmp == null)
        tmp = new TypedValue();
    resolveAttribute(theme, attrId, tmp, true);
    if (tmp.type >= TypedValue.TYPE_FIRST_COLOR_INT && tmp.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        return tmp.data;
    } else {//w  w w. j a va2 s  . c  o  m
        return defaultValue;
    }
}

From source file:com.glacialsoftware.googolplex.GoogolplexPreferenceFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    TypedValue typedValue = new TypedValue();
    getActivity().getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true);

    if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
            && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        int backgroundColor = typedValue.data;
        view.setBackgroundColor(backgroundColor);

    } else {/*from w  w  w  . j  av a2s.  co m*/
        Drawable drawable = getActivity().getResources().getDrawable(typedValue.resourceId);
        view.setBackgroundDrawable(drawable);
    }

    return view;
}

From source file:android.content.res.VectorResources.java

@Override
Drawable loadDrawable(TypedValue value, int id) throws NotFoundException {
    boolean isColorDrawable = false;
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        isColorDrawable = true;/*from   w ww . j  a  va2s.  co m*/
    }
    final long key = isColorDrawable ? value.data : (((long) value.assetCookie) << 32) | value.data;

    Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);

    if (dr != null) {
        return dr;
    }

    if (isColorDrawable) {
        dr = new ColorDrawable(value.data);
    }

    if (dr == null) {
        if (value.string == null) {
            throw new NotFoundException("Resource is not a Drawable (color or path): " + value);
        }

        String file = value.string.toString();

        if (file.endsWith(".xml")) {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                XmlResourceParser rp = getXml(id);
                // XmlResourceParser rp = loadXmlResourceParser(
                //         file, id, value.assetCookie, "drawable");
                dr = createDrawableFromXml(rp);
                rp.close();
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);

        } else {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                InputStream is = openRawResource(id, value);
                //InputStream is = mAssets.openNonAsset(
                //        value.assetCookie, file, AssetManager.ACCESS_STREAMING);
                //                System.out.println("Opened file " + file + ": " + is);
                dr = Drawable.createFromResourceStream(this, value, is, file, null);
                is.close();
                //                System.out.println("Created stream: " + dr);
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
    }
    Drawable.ConstantState cs;
    if (dr != null) {
        dr.setChangingConfigurations(value.changingConfigurations);
        cs = dr.getConstantState();
        if (cs != null) {
            synchronized (mAccessLock) {
                //Log.i(TAG, "Saving cached drawable @ #" +
                //        Integer.toHexString(key.intValue())
                //        + " in " + this + ": " + cs);
                if (isColorDrawable) {
                    mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                } else {
                    mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                }
            }
        }
    }

    return dr;
}

From source file:android.support.v7.content.res.AppCompatResources.java

private static boolean isColorInt(@NonNull Context context, @ColorRes int resId) {
    final Resources r = context.getResources();

    final TypedValue value = getTypedValue();
    r.getValue(resId, value, true);/*from  ww w. j ava  2  s. co  m*/

    return value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT;
}

From source file:com.franmontiel.fullscreendialog.FullScreenDialogFragment.java

private void setThemeBackground(View view) {
    TypedValue a = new TypedValue();
    getActivity().getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
    if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        view.setBackgroundColor(a.data);
    } else {/*  ww  w .  j ava2s .c  o m*/
        try {
            Drawable d = ResourcesCompat.getDrawable(getActivity().getResources(), a.resourceId,
                    getActivity().getTheme());
            ViewCompat.setBackground(view, d);
        } catch (Resources.NotFoundException ignore) {
        }
    }
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

public static void setCustomTheme(Context context, SparseIntArray customAttrs) {
    if (customAttrs == null || customAttrs.size() == 0) {
        currentAttrs = null;//from w  ww. j  av a 2  s . co m
        return;
    }

    TypedValue tmp = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, tmp, true);
    int textColorPrimaryOriginal = (tmp.type >= TypedValue.TYPE_FIRST_COLOR_INT
            && tmp.type <= TypedValue.TYPE_LAST_COLOR_INT) ? tmp.data : Color.TRANSPARENT;
    int textColorPrimaryOverridden = customAttrs.get(android.R.attr.textColorPrimary, textColorPrimaryOriginal);

    try {
        processWindow(context, customAttrs, textColorPrimaryOriginal, textColorPrimaryOverridden);
    } catch (Exception e) {
        Logger.e(TAG, e);
    }

    CustomThemeHelper instance = new CustomThemeHelper(context, customAttrs, textColorPrimaryOriginal,
            textColorPrimaryOverridden);
    LayoutInflaterCompat.setFactory(instance.inflater, instance);
    currentAttrs = customAttrs;
}

From source file:com.nttec.everychan.ui.tabs.TabsAdapter.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView == null ? inflater.inflate(R.layout.sidebar_tabitem, parent, false) : convertView;
    View dragHandler = view.findViewById(R.id.tab_drag_handle);
    ImageView favIcon = (ImageView) view.findViewById(R.id.tab_favicon);
    TextView title = (TextView) view.findViewById(R.id.tab_text_view);
    ImageView closeBtn = (ImageView) view.findViewById(R.id.tab_close_button);

    dragHandler.getLayoutParams().width = position == draggingItem ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
    dragHandler.setLayoutParams(dragHandler.getLayoutParams());

    if (position == selectedItem) {
        TypedValue typedValue = ThemeUtils.resolveAttribute(context.getTheme(), R.attr.sidebarSelectedItem,
                true);//from www  .j a  v  a2 s. c  om
        if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
                && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            view.setBackgroundColor(typedValue.data);
        } else {
            view.setBackgroundResource(typedValue.resourceId);
        }
    } else {
        view.setBackgroundColor(Color.TRANSPARENT);
    }

    TabModel model = this.getItem(position);

    switch (model.type) {
    case TabModel.TYPE_NORMAL:
    case TabModel.TYPE_LOCAL:
        closeBtn.setVisibility(View.VISIBLE);
        String titleText = model.title;
        if (model.unreadPostsCount > 0 || model.autoupdateError) {
            StringBuilder titleStringBuilder = new StringBuilder();
            if (model.unreadSubscriptions)
                titleStringBuilder.append("[*] ");
            if (model.autoupdateError)
                titleStringBuilder.append("[X] ");
            if (model.unreadPostsCount > 0)
                titleStringBuilder.append('[').append(model.unreadPostsCount).append("] ");
            titleText = titleStringBuilder.append(titleText).toString();
        }
        title.setText(titleText);
        ChanModule chan = MainApplication.getInstance().getChanModule(model.pageModel.chanName);
        Drawable icon = chan != null ? chan.getChanFavicon()
                : ResourcesCompat.getDrawable(context.getResources(), android.R.drawable.ic_delete, null);
        if (icon != null) {
            if (model.type == TabModel.TYPE_LOCAL) {
                Drawable[] layers = new Drawable[] { icon, ResourcesCompat.getDrawable(context.getResources(),
                        R.drawable.favicon_overlay_local, null) };
                icon = new LayerDrawable(layers);
            }
            /* XXX
             ?       ( overlay  favicon    ),
             ?   ?? ,   ,     (   ).
            ?    ? ? , , ? -? -  ,    ?.
               ??  ?,   ? ?  .
                    
            else if (model.type == TabModel.TYPE_NORMAL && model.pageModel != null &&
                    model.pageModel.type == UrlPageModel.TYPE_THREADPAGE && model.autoupdateBackground &&
                    MainApplication.getInstance().settings.isAutoupdateEnabled() &&
                    MainApplication.getInstance().settings.isAutoupdateBackground()) {
                Drawable[] layers = new Drawable[] {
                        icon, ResourcesCompat.getDrawable(context.getResources(), R.drawable.favicon_overlay_autoupdate, null) };
                icon = new LayerDrawable(layers);
            }
            */
            favIcon.setImageDrawable(icon);
            favIcon.setVisibility(View.VISIBLE);
        } else {
            favIcon.setVisibility(View.GONE);
        }
        break;
    default:
        closeBtn.setVisibility(View.GONE);
        title.setText(R.string.error_deserialization);
        favIcon.setVisibility(View.GONE);
    }

    closeBtn.setTag(position);
    closeBtn.setOnClickListener(onCloseClick);
    return view;
}