Example usage for android.content.res Resources getDimension

List of usage examples for android.content.res Resources getDimension

Introduction

In this page you can find the example usage for android.content.res Resources getDimension.

Prototype

public float getDimension(@DimenRes int id) throws NotFoundException 

Source Link

Document

Retrieve a dimensional for a particular resource ID.

Usage

From source file:Main.java

public static int getDip(Resources res, int resId) {
    return Math.round(res.getDimension(resId));
}

From source file:Main.java

public static float getPxFromDimen(Context context, String resName) {
    Resources resources = context.getResources();
    int resId = resources.getIdentifier(resName, "dimen", context.getPackageName());
    if (resId <= 0)
        return -1;
    return resources.getDimension(resId);
}

From source file:Main.java

/**
 * Get a dimension for the specified orientation.
 * This method may be heavy since it updates the {@code resources} twice.
 *//*  ww  w.j a  v  a 2s . c  o m*/
public static float getDimensionForOrientation(Resources resources, int id, int orientation) {
    Configuration configuration = resources.getConfiguration();
    if (configuration.orientation == orientation) {
        return resources.getDimension(id);
    }

    Configuration originalConfiguration = new Configuration(resources.getConfiguration());
    try {
        configuration.orientation = orientation;
        resources.updateConfiguration(configuration, null);
        return resources.getDimension(id);
    } finally {
        resources.updateConfiguration(originalConfiguration, null);
    }
}

From source file:Main.java

/**
 * Returns a Bitmap representing the thumbnail of the specified Bitmap. The
 * size of the thumbnail is defined by the dimension
 * android.R.dimen.launcher_application_icon_size.
 * <p>//from   ww w. ja  v a2s .c o  m
 * This method is not thread-safe and should be invoked on the UI thread
 * only.
 *
 * @param bitmap  The bitmap to get a thumbnail of.
 * @param context The application's context.
 * @return A thumbnail for the specified bitmap or the bitmap itself if the
 * thumbnail could not be created.
 */
public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) {
    int sIconWidth = -1;
    int sIconHeight = -1;
    final Resources resources = context.getResources();
    sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);

    final Paint sPaint = new Paint();
    final Rect sBounds = new Rect();
    final Rect sOldBounds = new Rect();
    Canvas sCanvas = new Canvas();

    int width = sIconWidth;
    int height = sIconHeight;

    sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    if (width > 0 && height > 0) {
        if (width < bitmapWidth || height < bitmapHeight) {
            final float ratio = (float) bitmapWidth / bitmapHeight;

            if (bitmapWidth > bitmapHeight) {
                height = (int) (width / ratio);
            } else if (bitmapHeight > bitmapWidth) {
                width = (int) (height * ratio);
            }

            final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig()
                    : Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
            sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
            canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
            return thumb;
        } else if (bitmapWidth < width || bitmapHeight < height) {
            final Config c = Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint);
            return thumb;
        }
    }

    return bitmap;
}

From source file:org.y20k.trackbook.helpers.NotificationHelper.java

private static Bitmap getNotificationIconLarge(boolean tracking) {

    // get dimensions
    Resources resources = mService.getResources();
    int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
    int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);

    Bitmap bitmap;//from   ww  w  . java  2s . c  o m
    if (tracking) {
        bitmap = getBitmap(R.drawable.ic_notification_large_tracking_48dp);
    } else {
        bitmap = getBitmap(R.drawable.ic_notification_large_not_tracking_48dp);
    }

    return Bitmap.createScaledBitmap(bitmap, width, height, false);
}

From source file:com.wizardsofm.deskclock.Utils.java

/**
 * Uses {@link Utils#calculateRadiusOffset(float, float, float)} after fetching the values
 * from the resources.//from  w  w w  .  j  a  v  a 2s .  co  m
 */
public static float calculateRadiusOffset(Resources resources) {
    if (resources != null) {
        float strokeSize = resources.getDimension(com.wizardsofm.deskclock.R.dimen.circletimer_circle_size);
        float dotStrokeSize = resources.getDimension(com.wizardsofm.deskclock.R.dimen.circletimer_dot_size);
        float markerStrokeSize = resources
                .getDimension(com.wizardsofm.deskclock.R.dimen.circletimer_marker_size);
        return calculateRadiusOffset(strokeSize, dotStrokeSize, markerStrokeSize);
    } else {
        return 0f;
    }
}

From source file:com.android.deskclock.Utils.java

/**
 * Uses {@link Utils#calculateRadiusOffset(float, float, float)} after fetching the values
 * from the resources./*w w w  .ja  v  a2  s  .com*/
 */
public static float calculateRadiusOffset(Resources resources) {
    if (resources != null) {
        float strokeSize = resources.getDimension(R.dimen.circletimer_circle_size);
        float dotStrokeSize = resources.getDimension(R.dimen.circletimer_dot_size);
        float markerStrokeSize = resources.getDimension(R.dimen.circletimer_marker_size);
        return calculateRadiusOffset(strokeSize, dotStrokeSize, markerStrokeSize);
    } else {
        return 0f;
    }
}

From source file:Main.java

public static Object getResource(Context context, Field field, int value) {
    Resources resources = context.getResources();
    Class type = field.getType();

    if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return resources.getBoolean(value);
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
        return resources.getInteger(value);
    } else if (type.isAssignableFrom(ColorStateList.class))
        return resources.getColorStateList(value);
    else if (type.isAssignableFrom(XmlResourceParser.class))
        return resources.getXml(value);
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return resources.getDimension(value);
    else if (type.isAssignableFrom(Drawable.class))
        return resources.getDrawable(value);
    else if (type.isAssignableFrom(Animation.class))
        return AnimationUtils.loadAnimation(context, value);
    else if (type.isAssignableFrom(Movie.class))
        return resources.getMovie(value);
    else if (type.isAssignableFrom(String.class))
        return resources.getString(value);
    else if (type.isArray()) {
        if (type.getName().equals("[I")) {
            return resources.getIntArray(value);
        } else if (type.isAssignableFrom(String[].class)) {
            return resources.getStringArray(value);
        }/*from   ww  w.jav a  2s.c om*/
    }

    return null;
}

From source file:com.linkbubble.util.Util.java

private static void initStatics(Context context) {
    final Resources resources = context.getResources();
    final DisplayMetrics metrics = resources.getDisplayMetrics();
    final float density = metrics.density;

    sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
    sIconTextureWidth = sIconTextureHeight = sIconWidth;
}

From source file:com.ticketmaster.servos.util.picasso.RoundedBitmapTransform.java

public RoundedBitmapTransform(Resources res, @DimenRes int cornerRadiusResId) {
    this(res, res.getDimension(cornerRadiusResId));
}