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 BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*from   w  w w  .  j  ava  2  s  .  c o m*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:Main.java

public static Drawable createGradientColorAndCover(Context context, int coveredResID, int CoverColor) {
    //final Resources res=context.getResources();
    final Resources res = context.getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, coveredResID);
    if (bitmap == null) {
        return null;
    }/* ww w. j  a  v  a2s  .c  o  m*/
    final int length = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int[] shape = new int[length * height];
    int[] colors = new int[length * height];
    bitmap.getPixels(shape, 0, length, 0, 0, length, height);
    int color = CoverColor;
    for (int i = 0; i < length * height; i++) {
        float percent = ((float) i % length / length) * 0xff;
        int alpha = ((int) percent << 6 * 4);
        alpha &= shape[i] & 0xFF000000;
        colors[i] = (alpha) | (color & 0x00FFFFFF);
    }
    Bitmap newbitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    newbitmap.setPixels(colors, 0, length, 0, 0, length, height);
    Bitmap fooBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(fooBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.drawBitmap(newbitmap, 0, 0, paint);
    newbitmap.recycle();
    bitmap.recycle();
    return new BitmapDrawable(res, fooBitmap);
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }/*from   w w w . ja v a 2 s  .  co m*/
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #808080
    paint.setColor(Color.rgb(127, 127, 127));
    // text size in pixels
    paint.setTextSize((int) (14 * scale * 5));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    //      int x = (bitmap.getWidth() - bounds.width()) / 2;
    //      int y = (bitmap.getHeight() + bounds.height()) / 2;
    //draw  text  to the bottom
    int x = (bitmap.getWidth() - bounds.width()) / 10 * 9;
    int y = (bitmap.getHeight() + bounds.height()) / 10 * 9;
    canvas.drawText(gText, x, y, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap getPlusBitmap(Context context, String s) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from ww  w  .  j  a  v a 2  s .  c  o  m*/
    Bitmap bitmap = BitmapFactory.decodeStream(getCommunityPicInputStream(s));
    Bitmap bitmap1;
    if (bitmap != null) {
        Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), 0x7f02003f);
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
        canvas.drawBitmap(bitmap2, bitmap.getWidth() - bitmap2.getWidth() / 2,
                bitmap.getHeight() - bitmap2.getHeight() / 2, paint);
        bitmap2.recycle();
    } else {
        bitmap1 = null;
    }
    return bitmap1;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {//w w w .  ja v  a 2  s.c  o m
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.rgb(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

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

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static void notify(Context context, int id, String title, String detail) {
    Notification notification = new NotificationCompat.Builder(context).setContentTitle(title).setTicker(detail)
            .setContentText(detail).setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setLargeIcon(/*from  w  w w.  ja  va  2s. c o m*/
                    BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_dialog_alert))
            .build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, notification);
}

From source file:Main.java

/**
 * Create bitmap image from resource/* www  .j  av a 2s .  c om*/
 *
 * @param context
 * @param iconName
 * @param widthDip
 * @param heightDip
 * @return
 */
public static Bitmap resizeMapIcons(Context context, String iconName, int widthDip, int heightDip) {
    int widthPx = (int) dipToPixels(context, widthDip);
    int heightPx = (int) dipToPixels(context, heightDip);

    Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(),
            context.getResources().getIdentifier(iconName, "drawable", context.getPackageName()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, widthPx, heightPx, false);
    return resizedBitmap;
}

From source file:Main.java

/**
 * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
 *
 * @param resources The current resources.
 * @param resourceId The resource ID of the drawable to rotate.
 * @param angle The angle of rotation.// w w  w  .ja v  a 2s  . com
 * @return Rotated drawable.
 */
public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId,
        float angle) {

    // Get the original drawable and make a copy which will be rotated.
    Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
    Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform the rotation.
    Canvas tempCanvas = new Canvas(rotated);
    tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2);
    tempCanvas.drawBitmap(original, 0, 0, null);

    return new BitmapDrawable(resources, rotated);
}

From source file:Main.java

public static Bitmap decodeResource(Context context, int resId) {
    return BitmapFactory.decodeResource(context.getResources(), resId);
}

From source file:Main.java

public static Bitmap getBitmap(Context context, int imgres) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imgres);
    return bitmap;
}