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

/**
 * @Description:/*from   www.  j av  a2s .  com*/
 * @param applicationContext
 * @return
 */
public static int getHeightActionBar(Context context) {
    int height;
    TypedValue typeValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.actionBarSize, typeValue, true);
    height = TypedValue.complexToDimensionPixelSize(typeValue.data, context.getResources().getDisplayMetrics());
    return height;
}

From source file:Main.java

public static int getActionBarHeightInDp(Context context) {
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    final DisplayMetrics dm = context.getResources().getDisplayMetrics();
    if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
            actionBarHeight = (int) TypedValue.complexToFloat(tv.data);
    } else {//from   ww w.  j  ava2 s.c  o m
        tv.data = 48;
        actionBarHeight = (int) TypedValue.complexToFloat(tv.data);
    }
    return actionBarHeight;
}

From source file:Main.java

public static int getColorFromContext(Context context, int color_id) {
    int[] textSizeAttr = new int[] { color_id };
    TypedValue typedValue = new TypedValue();
    TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
    int color = a.getColor(0, 0);
    a.recycle();/*from   w  w w.  ja v  a2s.c o  m*/
    return color;
}

From source file:Main.java

@TargetApi(14)
public static int getActionBarHeight(Context context) {
    int result = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        TypedValue tv = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
        result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }//w  ww.java 2 s  .co m
    return result;
}

From source file:Main.java

public static String getColorFromThemeInHex(Context context, int resourceId) {
    TypedValue typedvalueattr = new TypedValue();
    context.getTheme().resolveAttribute(resourceId, typedvalueattr, true);
    return String.format("#%06X", (0xFFFFFF & context.getResources().getColor(typedvalueattr.resourceId)));
}

From source file:Main.java

public static Bitmap getBitmapForDensity(Resources res, int displayDpi, int resId) {
    try {//from  w w w.j  a va2s .c om
        TypedValue value = new TypedValue();
        res.getValueForDensity(resId, displayDpi, value, true);
        AssetFileDescriptor fd = res.getAssets().openNonAssetFd(value.assetCookie, value.string.toString());
        Options opt = new Options();
        opt.inTargetDensity = displayDpi;
        Bitmap bitmap = BitmapFactory.decodeResourceStream(res, value, fd.createInputStream(), null, opt);
        bitmap.setDensity(res.getDisplayMetrics().densityDpi);
        fd.close();
        return bitmap;
    } catch (Exception e) {
        return BitmapFactory.decodeResource(res, resId);
    }
}

From source file:Main.java

/**
 * gets float from resource values/*from ww w.  j  a v a 2 s  .  com*/
 *
 * @param context context of calling activity
 * @param dimen   resource id
 * @return float
 */
public static float getFloat(Context context, @DimenRes int dimen) {
    TypedValue outValue = new TypedValue();
    context.getResources().getValue(dimen, outValue, true);
    return outValue.getFloat();
}

From source file:Main.java

@SuppressWarnings("deprecation")
private static int getColor(Context context, int id, int defaultValue) {
    if (value == null) {
        value = new TypedValue();
    }/*from  w  ww . jav  a 2s.  com*/

    try {
        Resources.Theme theme = context.getTheme();
        if (theme != null && theme.resolveAttribute(id, value, true)) {
            if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
                return value.data;
            } else if (value.type == TypedValue.TYPE_STRING) {
                return context.getResources().getColor(value.resourceId);
            }
        }
    } catch (Exception ignored) {
    }
    return defaultValue;
}

From source file:Main.java

/**
 * Returns an int array with the color values for the given attributes (R.attr).
 * Any unresolved colors will be represented by -1
 *//*from w ww  . ja  v  a 2 s .c  o m*/
public static int[] getThemeColorIDs(final Context context, final int[] attrs) {
    int[] colors = new int[attrs.length];
    Resources.Theme theme = context.getTheme();
    for (int i = 0; i < attrs.length; i++) {
        TypedValue typedValue = new TypedValue();
        if (theme.resolveAttribute(attrs[i], typedValue, true)) {
            colors[i] = typedValue.data;
        } else {
            colors[i] = -1;
        }
    }
    return colors;
}

From source file:Main.java

public static int getThemeColor(Context ctx, int attr) {
    TypedValue tv = new TypedValue();
    if (ctx.getTheme().resolveAttribute(attr, tv, true)) {
        return tv.data;
    }/*from  ww  w  . j a  v  a  2 s  . co m*/
    return 0;
}