Example usage for android.util TypedValue TypedValue

List of usage examples for android.util TypedValue TypedValue

Introduction

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

Prototype

TypedValue

Source Link

Usage

From source file:Main.java

/**
 * Resolves the given attribute id of the theme to a resource id
 *//*from w  w w. ja  v  a 2 s  .c o  m*/
public static int getAttribute(Theme theme, int attrId) {
    final TypedValue outValue = new TypedValue();
    theme.resolveAttribute(attrId, outValue, true);
    return outValue.resourceId;
}

From source file:Main.java

/**
 * Gets the action bar height in pixels// w w w . j  a v  a  2 s  .  co  m
 *
 * @param context
 * @return action bar height in pixels
 */
public static int getActionBarHeight(Context context) {
    TypedValue tv = new TypedValue();
    View view = new View(context);
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }

    return 0;
}

From source file:Main.java

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context     Activity Context//from w ww  . j  av a2s. c o m
 * @param styleAttrId Theme style id
 * @param attributeId if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1)
        return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId,
            getAttributeArray(attributeId));
    try {
        String font = typedArray.getString(0);
        return font;
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        typedArray.recycle();
    }
}

From source file:Main.java

public static float getAttributeDimension(final Context context, final Resources.Theme theme, final int resId) {
    final TypedValue typedValue = new TypedValue(); // create a new typed value to received the resolved attribute
    // value/*from  w  w  w  .  j a v  a 2  s  . co m*/
    final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    if (!theme.resolveAttribute(resId, typedValue, true)) // if we can't resolve the value
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId));
    }
    if (typedValue.type != TypedValue.TYPE_DIMENSION) // if the value isn't of the correct type
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId) + " type #0x"
                + Integer.toHexString(typedValue.type) + " is not valid");
    }
    return typedValue.getDimension(displayMetrics); // return the value of the attribute in terms of the display
}

From source file:Main.java

public static Bitmap compressBitmap(Context context, int resId, int maxWidth, int maxHeight) {
    checkParam(context);// w w w.  j  a  va 2 s  .c o  m
    final TypedValue value = new TypedValue();
    InputStream is = null;
    try {
        is = context.getResources().openRawResource(resId, value);
        return compressBitmap(context, is, maxWidth, maxHeight);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static int[] getIntArray(int resId, Resources mResources) {
    TypedArray array = mResources.obtainTypedArray(resId);
    int[] rc = new int[array.length()];
    TypedValue value = new TypedValue();
    for (int i = 0; i < array.length(); i++) {
        array.getValue(i, value);/*  w w w. ja v  a 2 s. co m*/
        rc[i] = value.resourceId;
    }
    return rc;
}

From source file:com.fa.mastodon.util.ThemeUtils.java

public static Drawable getDrawable(Context context, @AttrRes int attribute, @DrawableRes int fallbackDrawable) {
    TypedValue value = new TypedValue();
    @DrawableRes//  ww  w.  j a  v a  2 s  .co m
    int resourceId;
    if (context.getTheme().resolveAttribute(attribute, value, true)) {
        resourceId = value.resourceId;
    } else {
        resourceId = fallbackDrawable;
    }
    return ContextCompat.getDrawable(context, resourceId);
}

From source file:Main.java

public static int getDrawableRes(Theme theme, @AttrRes int attr) {
    final TypedValue out = new TypedValue();
    theme.resolveAttribute(attr, out, true);
    return out.resourceId;
}

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;/*from w w  w  .ja v a  2s  .co m*/
    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 {//from w  ww.  ja  v  a  2  s  .  c  om
        return context.getResources().getColor(typedValue.resourceId);
    }
}