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) 

Source Link

Document

Synonym for #decodeResource(Resources,int,android.graphics.BitmapFactory.Options) with null Options.

Usage

From source file:Main.java

public static Bitmap getDefaultBitmap(Bitmap target, Resources res, @DrawableRes int id) {
    if (target instanceof Bitmap) {
        return target;
    } else {//from w ww  .j  a  v a2  s .  co m
        return BitmapFactory.decodeResource(res, id);
    }
}

From source file:Main.java

public static Drawable getBitmapDrawable(Context ctx, int value) {
    Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), value);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(ctx.getResources(), bmp);
    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    return bitmapDrawable;
}

From source file:Main.java

/** simply resizes a given drawable resource to the given width and height */
public static Drawable resizeImage(Context ctx, int resId, int iconWidth, int iconHeight) {

    // load the origial Bitmap
    Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(), resId);

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = iconWidth;
    int newHeight = iconHeight;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);

    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}

From source file:Main.java

public static Point getImageMaxDimension(Context context, int[] imgRes) {
    final Point point = new Point();

    for (int i = 0, length = imgRes.length; i < length; i++) {
        Bitmap tmp = BitmapFactory.decodeResource(context.getResources(), imgRes[i]);
        int width = tmp.getWidth();
        int height = tmp.getHeight();
        tmp.recycle();//from   w w  w.ja v a  2  s .  com
        tmp = null;

        if (point.x < width) {
            point.x = width;
        }

        if (point.y < height) {
            point.y = height;
        }
    }

    return point;
}

From source file:Main.java

/**
 * Call for loading image from application resources
 * /*from   www .j  a va 2s  . c  o  m*/
 * @param res
 *            application resources object
 * @param id
 *            resource id
 * @return requested Bitmap
 */
public static Bitmap getBitmapImageById(Resources res, int id) {
    return BitmapFactory.decodeResource(res, id);
}

From source file:Main.java

public static Bitmap getAppIcon(Context context) {
    try {//w  w  w  . j  a  v a 2  s.c  om
        Class<?> drawableClass = Class.forName(context.getPackageName() + ".R$drawable");
        Field f = drawableClass.getField("ic_launcher");
        return BitmapFactory.decodeResource(context.getResources(), f.getInt(null));
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/** return NinePatchDrawable with give id
 * @param aContext//  w w w .jav  a  2  s .co  m
 * @param aNinePatchResourceId
 * @return
 */
public static NinePatchDrawable getNinePatchDrawable(Context aContext, int aNinePatchResourceId) {
    Bitmap bg = BitmapFactory.decodeResource(aContext.getResources(), aNinePatchResourceId);
    NinePatch np = new NinePatch(bg, bg.getNinePatchChunk(), null);
    NinePatchDrawable t9Patch = new NinePatchDrawable(aContext.getResources(), np);
    return t9Patch;
}

From source file:Main.java

/**
 * /*w  w w  .j  ava2 s . com*/
 * @return Resource's RGBA byte array
 */
public static byte[] getImageRGBA(Resources res, int resouceId) {
    Bitmap bitmap = BitmapFactory.decodeResource(res, resouceId);
    if (bitmap.getWidth() != 32 || bitmap.getHeight() != 32) {
    }
    return getImageRGBA(bitmap);
}

From source file:Main.java

/***
 * //from  w  ww .  ja  v  a 2  s .  c  o  m
 * @param imageId The image from layout
 * @param resources From which activity
 * @return Serialized image
 */
public static byte[] ImageToByte(int imageId, Resources resources) {
    Bitmap bitmap = BitmapFactory.decodeResource(resources, imageId);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context context, int resId, String text) {

    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null)
        bitmapConfig = Bitmap.Config.ARGB_8888;
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(context.getResources().getColor(android.R.color.white));
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    paint.setTextSize((int) (12 * scale));

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 4;
    int y = (bitmap.getHeight() + bounds.height()) / 5;

    canvas.drawText(text, x * scale, y * scale, paint);

    return bitmap;
}