Example usage for android.graphics Bitmap createBitmap

List of usage examples for android.graphics Bitmap createBitmap

Introduction

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

Prototype

public static Bitmap createBitmap(@Nullable DisplayMetrics display, @NonNull @ColorInt int colors[], int width,
        int height, @NonNull Config config) 

Source Link

Document

Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

Usage

From source file:Main.java

/**
 * Method to create a fully-transparent Bitmap using the same size of the source passed as
 * parameter and also the same density./*from  ww w  .  ja v  a 2  s  . co m*/
 *
 * @param source The original Bitmap.
 * @return A transparent Bitmap with the same size of the source.
 */
public static Bitmap clear(Bitmap source) {
    Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight());
    //to erase the color from a Bitmap, I must use a mutable Bitmap!!!!
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    mutableBitmap.eraseColor(Color.TRANSPARENT);
    return mutableBitmap;
}

From source file:Main.java

/**
 * TODO write documentation// ww  w  .  j a  v  a 2  s  . co  m
 *
 * @param sourceBitmap
 * @param color
 * @return
 */
public static Bitmap overlayColor(Bitmap sourceBitmap, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),
            sourceBitmap.getHeight());
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);
    return mutableBitmap;
}

From source file:Main.java

public static Bitmap crop(Bitmap bitmap, Rect cropRect) {
    return Bitmap.createBitmap(bitmap, cropRect.left, cropRect.top, cropRect.width(), cropRect.height());
}

From source file:Main.java

/**
 * center crop to target size./*from w  w  w.  j  av  a 2  s  .  c  om*/
 */
public static Bitmap centerCropBitmap(Bitmap srcBmp, int destSize) {
    int srcWidth = srcBmp.getWidth();
    int srcHeight = srcBmp.getHeight();
    if (srcWidth >= srcHeight) {
        destSize = destSize <= srcHeight ? destSize : srcHeight;
    } else {
        destSize = destSize <= srcWidth ? destSize : srcWidth;
    }
    return Bitmap.createBitmap(srcBmp, srcWidth / 2 - destSize / 2, srcHeight / 2 - destSize / 2, destSize,
            destSize);
}

From source file:Main.java

public static Bitmap captureWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);//from w  w w  . j a  va  2  s.  c o m
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    view.destroyDrawingCache();
    return ret;
}

From source file:Main.java

public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);//from ww  w  .  ja  va 2 s  .c o  m
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int[] screen = getWidthAndHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, screen[0], screen[1]);
    view.destroyDrawingCache();
    return bp;

}

From source file:Main.java

/**
 * ATTENTION: DON'T USE THIS METHOD BECAUSE IT HAS BAD PERFORMANCES.
 *
 * @param source The original Bitmap.//  ww w  . j  a va 2s. c  o  m
 * @param color  Color to overlay.
 * @return the result image.
 */
@Deprecated
private static Bitmap overlayColor(Bitmap source, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight());
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);
    return mutableBitmap;
}

From source file:Main.java

public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);//from   www.  ja v  a 2  s.c o m
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();
    return bp;
}

From source file:Main.java

public static Bitmap snapShotWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);/*  w  w w .ja v a  2s. c  om*/
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;

}