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

@Deprecated
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;//www. j a  va  2 s  .  c  om
    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 synchronized 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  w ww. j  a  v a2  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 bitmap from resource id//w w  w.j a va  2 s.c o  m
 */
public static Bitmap res2bitmap(int resId, BitmapFactory.Options opt) {
    return BitmapFactory.decodeResource(sApp.getResources(), resId, opt);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// www  .jav a 2s . c o m
    BitmapFactory.decodeResource(context.getResources(), resId, options);

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

    Log.d("----> Sample Size:", sampleSize + "");
    if (sampleSize < 1) { // return a scaled UP version of image
        Bitmap bg = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bg, (int) (bg.getWidth() / sampleSize),
                (int) (bg.getHeight() / sampleSize), true);
        bg.recycle();
        return scaledBitmap;
    } else { // return a scaled DOWN version of image
        options.inSampleSize = Math.round(sampleSize);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void setImageBackground(View view, int width, int height, int ResId, Activity context) {
    Options opts = new Options();
    opts.outHeight = height;//from ww  w. j a v  a 2 s .  c om
    opts.outWidth = width;
    opts.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResId, opts);
    view.setBackgroundDrawable(new BitmapDrawable(bitmap));
}

From source file:Main.java

public static Bitmap getSizedBitmap(@NonNull Resources res, @DrawableRes int resId, int desiredWidth) {
    // Load just the size of the image
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w ww.j a va 2s  .  c  o  m
    BitmapFactory.decodeResource(res, resId, options);

    // Options now has the bounds; prepare it for getting the actual image
    options.inSampleSize = 1;
    options.inJustDecodeBounds = false;

    if (options.outWidth > desiredWidth) {
        final int halfWidth = options.outWidth / 2;

        while (halfWidth / options.inSampleSize > desiredWidth) {
            options.inSampleSize *= 2;
        }
    }

    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static Bitmap bitmapLoad(Resources res, int resId, int width, int height) {

    // calc scale, load appropriately sampled bitmap from given resource

    BitmapFactory.Options resOptions = new BitmapFactory.Options();
    resOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, resOptions);

    int resHeight = resOptions.outHeight;
    int resWidth = resOptions.outWidth;

    float xScale = (float) width / (float) resWidth;
    float yScale = (float) height / (float) resHeight;
    float scale = Math.max(xScale, yScale);
    if (scale > 1)

        if (width == 0)
            width = Math.round(resWidth / scale);
        else if (height == 0)
            height = Math.round(resHeight / scale);

    resOptions.inSampleSize = sampleSize(scale);
    resWidth /= resOptions.inSampleSize;
    resHeight /= resOptions.inSampleSize;
    resOptions.inJustDecodeBounds = false;

    Bitmap rawBitmap = BitmapFactory.decodeResource(res, resId, resOptions);

    // compare aspect ratio and crop

    rawBitmap = bitmapCrop(rawBitmap, width, height, resWidth, resHeight);

    // scale to desired size

    return Bitmap.createScaledBitmap(rawBitmap, width, height, true);
}

From source file:Main.java

public static int loadTextureFromResource(Context context, int resId) {
    final int textureHandle[] = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);
    if (textureHandle[0] != 0) {
        final BitmapFactory.Options op = new BitmapFactory.Options();
        op.inScaled = false;/*  w w  w.j  a v  a  2  s .  c o  m*/
        final Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId, op);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
    } else {
        throw new RuntimeException("Error Loading Texture");
    }

    return textureHandle[0];
}

From source file:Main.java

public static Bitmap getDrawableAsBitmap(Context context, int drawable) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        options.inMutable = true;//from   ww  w  .  j  a  va  2s . c  o  m
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Resources res = context.getResources();
    return BitmapFactory.decodeResource(res, drawable, options);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidthInPixel,
        int reqHeightInPixel) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   ww  w. j a  v  a2s.  com*/
    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);
}