Example usage for android.graphics Bitmap getConfig

List of usage examples for android.graphics Bitmap getConfig

Introduction

In this page you can find the example usage for android.graphics Bitmap getConfig.

Prototype

public final Config getConfig() 

Source Link

Document

If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.

Usage

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;
    paint.setAntiAlias(true);/* w ww  .j a  v a2 s .  co  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2, float left, float top) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, left, top, null);
    return bmOverlay;
}

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;
}

From source file:Main.java

public static Bitmap getPaintedBitmap(Bitmap bitmap, Paint paint, boolean deleteOld) {
    final Bitmap colorBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    final Canvas c = new Canvas(colorBitmap);
    c.drawBitmap(bitmap, 0, 0, paint);/*from w  ww .  j a  v a  2  s  .c o  m*/
    if (deleteOld) {
        bitmap.recycle();
    }
    return colorBitmap;
}

From source file:Main.java

public static Bitmap scale(Bitmap b, float scale) {
    Bitmap res = Bitmap.createBitmap((int) (b.getWidth() * scale + .5f), (int) (b.getHeight() * scale + .5f),
            b.getConfig());
    Canvas c = new Canvas(res);
    c.scale(scale, scale);/*from   w ww .  j  ava  2  s . c o  m*/
    c.drawBitmap(b, 0, 0, scalePaint);
    return res;
}

From source file:Main.java

public static Bitmap combineTwoBitmaps(Bitmap background, Bitmap foreground) {
    Bitmap combinedBitmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(),
            background.getConfig());
    Canvas canvas = new Canvas(combinedBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(background, 0, 0, paint);
    canvas.drawBitmap(foreground, 0, 0, paint);
    return combinedBitmap;
}

From source file:Main.java

/**
 * Create a bitmap from a background and a foreground bitmap
 *
 * @param background The bitmap placed in the background
 * @param foreground The bitmap placed in the foreground
 * @return the merged bitmap//from   w ww . ja v a2 s.  co  m
 */
public static Bitmap mergeBitmap(@NonNull Bitmap background, @NonNull Bitmap foreground) {
    Bitmap result = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(background, 0f, 0f, null);
    canvas.drawBitmap(foreground, 10, 10, null);
    return result;
}

From source file:Main.java

public static Bitmap cropBitmap(Bitmap bitmap, Rect rect) {
    int w = rect.right - rect.left;
    int h = rect.bottom - rect.top;
    Bitmap ret = Bitmap.createBitmap(w, h, bitmap.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(bitmap, -rect.left, -rect.top, null);
    bitmap.recycle();/*from  w w  w.  j a  va2  s  .co m*/
    return ret;
}

From source file:Main.java

public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap drawTextCenterToBitmap(Bitmap bitmap, String text, int textSize, int textColor) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from w ww .  ja va2 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 - #3D3D3D
    paint.setColor(textColor);
    // text size in pixels
    paint.setTextSize(textSize);
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.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 * 5;
    int y = (bitmap.getHeight() + bounds.height()) / 10 * 5;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}