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(int width, int height, @NonNull Config config) 

Source Link

Document

Returns a mutable bitmap with the specified width and height.

Usage

From source file:Main.java

public static Bitmap getWebViewShotshatByPicture(WebView webView) {
    Picture picture = webView.capturePicture();
    int width = picture.getWidth();
    int height = picture.getHeight();
    Bitmap bw = null;/*from   www  .j ava 2 s .co m*/
    if (width > 0 && height > 0) {
        bw = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bw);
        //picture.draw(canvas);
        canvas.drawPicture(picture);
    }
    return bw;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);/*from   w  w  w. j a va2  s .c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap loadEmptyBitmap(Context context, String file_path) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  w  ww .  jav a 2  s. co m
    BitmapFactory.decodeFile(file_path, options);
    Bitmap empty_bitmap = Bitmap.createBitmap(options.outWidth, options.outHeight, Config.ALPHA_8);
    return empty_bitmap;
}

From source file:Main.java

public static Bitmap circleCrop(Bitmap bitmap) {
    int size = Math.min(bitmap.getWidth(), bitmap.getHeight());

    Bitmap output = Bitmap.createBitmap(size, size, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, size, size);

    paint.setAntiAlias(true);//from w  w w  . ja  v  a 2 s .c  o m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(size / 2, size / 2, size / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    // Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    // return _bmp;
    return output;
}

From source file:Main.java

public static Bitmap roundCorners(Bitmap bitmap, float radius) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w  w  w.jav a 2 s.c  o  m*/

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundAngleImage(Bitmap bitmap, int pixels, boolean recycleOld) {
    Bitmap output = null;/* w  w  w  .  j a v a  2  s . c o  m*/
    if (bitmap != null && !bitmap.isRecycled()) {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        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);
        paint.setFilterBitmap(true);
        canvas.drawARGB(0, 0, 0, 0);
        //            paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        if (recycleOld)
            bitmap.recycle();
        return output;
    }
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, final float roundPx) {
    if (bitmap == null)
        return null;
    try {/*w w w.j  a  v a 2  s  .  co m*/
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();

    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;/* ww  w.  java 2 s.  c om*/
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    if (bitmap == null) {
        return null;
    }//  w  w w . java 2  s .com

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    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);

    paint.setAntiAlias(true);
    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 drawBackground(int cellSize, int height, int widht) {

    Bitmap bitmap = Bitmap.createBitmap(widht, height, Config.ARGB_8888);
    Canvas cv = new Canvas(bitmap);

    Paint background = new Paint();
    background.setColor(BACKGROUND_COLOR);
    cv.drawRect(0, 0, widht, height, background);
    background.setAntiAlias(true);// w  w  w.  j a v  a2 s  . c om

    background.setColor(LINE_COLOR);

    for (int i = 0; i < widht / cellSize; i++) {
        cv.drawLine(cellSize * i, 0, cellSize * i, height, background);
    }

    for (int i = 0; i < height / cellSize; i++) {
        cv.drawLine(0, cellSize * i, widht, cellSize * i, background);
    }
    return bitmap;
}