Example usage for android.graphics BitmapFactory decodeResource

List of usage examples for android.graphics BitmapFactory decodeResource

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeResource.

Prototype

public static Bitmap decodeResource(Resources res, int id, Options opts) 

Source Link

Document

Synonym for opening the given resource and calling #decodeResourceStream .

Usage

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, float reqWidthInDip,
        float reqHeightInDip) {

    int reqWidthInPixel = (int) dipToPixels(res, reqWidthInDip);
    int reqHeightInPixel = (int) dipToPixels(res, reqHeightInDip);

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w ww.ja  v  a  2 s.  co m
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static Bitmap createJustBoundsBitmap(Resources res, int resId) {
    Bitmap bm = null;//  w ww  .jav  a  2s.  co m
    try {
        bm = BitmapFactory.decodeResource(res, resId, getJustBoundsOptions());
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return bm;
}

From source file:Main.java

/**
 * Decodes <tt>Bitmap</tt> identified by given <tt>resId</tt> scaled to
 * requested width and height./*  ww w .ja  v a 2 s .c  om*/
 * @param res the <tt>Resources</tt> object.
 * @param resId bitmap resource id.
 * @param reqWidth requested width.
 * @param reqHeight requested height.
 * @return  <tt>Bitmap</tt> identified by given <tt>resId</tt> scaled to
 *          requested width and height.
 */
public static Bitmap scaledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static BitmapFactory.Options getBounds(Resources res, int resourceId) {
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;//from w ww.  j  a  v  a 2 s. com
    BitmapFactory.decodeResource(res, resourceId, bounds);
    return bounds;
}

From source file:Main.java

/**
 * Loads a texture from a resource ID, returning the OpenGL ID for that
 * texture. Returns 0 if the load failed.
 * /*  w  ww.  java  2s .  c  o  m*/
 * @param context
 * @param resourceId
 * @return
 */
public static int loadTexture(Context context, int resourceId) {
    final int[] textureObjectIds = new int[1];
    glGenTextures(1, textureObjectIds, 0);

    if (textureObjectIds[0] == 0) {
        Log.w(TAG, "Could not generate a new OpenGL texture object.");
        return 0;
    }

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;

    // Read in the resource
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

    if (bitmap == null) {
        Log.w(TAG, "Resource ID " + resourceId + " could not be decoded.");
        glDeleteTextures(1, textureObjectIds, 0);
        return 0;
    }

    // Bind to the texture in OpenGL
    glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]);

    // Set filtering: a default must be set, or the texture will be
    // black.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Load the bitmap into the bound texture.
    texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);

    // Note: Following code may cause an error to be reported in the
    // ADB log as follows: E/IMGSRV(20095): :0: HardwareMipGen:
    // Failed to generate texture mipmap levels (error=3)
    // No OpenGL error will be encountered (glGetError() will return
    // 0). If this happens, just squash the source image to be
    // square. It will look the same because of texture coordinates,
    // and mipmap generation will work.

    glGenerateMipmap(GL_TEXTURE_2D);

    // Recycle the bitmap, since its data has been loaded into
    // OpenGL.
    bitmap.recycle();

    // Unbind from the texture.
    glBindTexture(GL_TEXTURE_2D, 0);

    return textureObjectIds[0];
}

From source file:Main.java

private static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
        BitmapFactory.Options options) {
    if (placeholderImageResource == 0) {
        return null;
    }/*  ww  w.  j a va  2s  . com*/
    return BitmapFactory.decodeResource(context.getResources(), placeholderImageResource, options);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  ww w.  j  av a 2 s.  c  o  m*/
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

/**
 * Decode a given drawable resource with the specified dimensions
 *
 * @param res the app resources// ww  w .j a  v  a  2  s.  c  om
 * @param resId the drawable resource ID
 * @param reqWidth the required width
 * @param reqHeight the required height
 * @return the bitmap
 */
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static Bitmap decodeScaleImage(Context context, int resId, int targetWidth, int targetHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*w  w w . j  av a2 s .  co  m*/
    BitmapFactory.decodeResource(context.getResources(), resId, options);
    options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId, options);
    return bitmap;
}

From source file:Main.java

public static Bitmap resIdToBitmap(Resources res, int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//w  w w .j ava2  s  .c om
    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPreferQualityOverSpeed = true;
    try {
        return BitmapFactory.decodeResource(res, resId, options);
    } catch (OutOfMemoryError e1) {
        e1.printStackTrace();
        return null;
    }
}