Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

In this page you can find the example usage for android.graphics Canvas drawBitmap.

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:Main.java

public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int width) {
    // TODO Auto-generated method stub
    int targetWidth = width;
    int targetHeight = width;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);
    canvas.clipPath(path);/* w  w w.j  a  v  a  2s  .  co m*/
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb,
        boolean rb) {
    Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx);
    Canvas canvas = new Canvas(roundCornerBitmap);
    canvas.drawARGB(0, 0, 0, 0);/*from  w  ww . j  a va  2 s  .  co m*/
    int bw = bitmap.getWidth();
    int bh = bitmap.getHeight();
    int centerW = bw / 2;
    int centerH = bh / 2;

    Paint paint = new Paint();
    Paint defaultPaint = new Paint();
    int color = 0xff424242;
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    paint.setAntiAlias(true);
    paint.setColor(color);

    Bitmap cutBitmap = null;
    Canvas cutCanvas = null;
    if (!lt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!lb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    return roundCornerBitmap;
}

From source file:Main.java

private static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean, int width, int heigth) {
    Bitmap localBitmap = Bitmap.createBitmap(width, heigth, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);
    int i;/*from ww w  .ja  va  2 s. c  o  m*/
    int j;
    if (bitmap.getHeight() > bitmap.getWidth()) {
        i = bitmap.getWidth();
        j = bitmap.getWidth();
    } else {
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
    while (true) {
        localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0, width, heigth), null);
        if (paramBoolean)
            bitmap.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            e.printStackTrace();
        }
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
}

From source file:Main.java

public static Bitmap getClip(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);/*from   w w  w .  j a  v  a 2  s.co  m*/
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap formatUserAvatar(Bitmap photo) {
    int maskColor = 0xff424242;
    Paint cornerPaint = new Paint();
    cornerPaint.setAntiAlias(true);//from   www  .  java2 s. com
    cornerPaint.setColor(maskColor);
    Rect roundedCornerRect = new Rect(0, 0, 256, 256);
    RectF roundedCornerRectF = new RectF(roundedCornerRect);
    Bitmap roundedCornerBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    Canvas roundedCornerCanvas = new Canvas(roundedCornerBitmap);
    roundedCornerCanvas.drawARGB(0, 0, 0, 0);
    roundedCornerCanvas.drawRoundRect(roundedCornerRectF, 128, 128, cornerPaint);
    cornerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(photo, 256, 256, true);
    roundedCornerCanvas.drawBitmap(scaledBitmap, roundedCornerRect, roundedCornerRect, cornerPaint);
    return roundedCornerBitmap;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);/*ww  w  .ja  va  2s. c  o m*/
    int w = src.getWidth();
    int h = src.getHeight();
    int canvasW = Math.round(radius * 2);
    Bitmap bitmap = Bitmap.createBitmap(canvasW, canvasW, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Path path = new Path();
    path.addCircle(radius, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect srcRect = new Rect(0, 0, w, h);
    Rect dstRect = new Rect(0, 0, canvasW, canvasW);

    canvas.drawBitmap(src, srcRect, dstRect, paint);

    return bitmap;
}

From source file:Main.java

/**Method to get round shaped bitmap. Mostly used for profile pictures
 *
 *     @param scaleBitmapImage/*from   www.j a  v  a  2  s. c  o m*/
 *                      The source bitmap which has to converted to round shape
 *     @param context
 *                      The context
 *     @param targetWidthPixels
 *                      The width required for the target or returned  bitmap
 *     @param targetHeightPixels
 *                      The height required for the target or returned  bitmap
 *     @return
 *            round shaped bitmap
 */
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, Context context, int targetWidthPixels,
        int targetHeightPixels) {

    Bitmap targetBitmap = Bitmap.createBitmap(targetWidthPixels, targetHeightPixels, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidthPixels - 1) / 2, ((float) targetHeightPixels - 1) / 2,
            (Math.min(((float) targetWidthPixels), ((float) targetHeightPixels)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidthPixels, targetHeightPixels), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bmp, float roundDP) {
    //      roundDP *= Constants.screen_density;
    Bitmap bmpOut = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
    Canvas c = new Canvas(bmpOut);
    final Paint p = new Paint();
    final RectF rectF = new RectF(0, 0, bmp.getWidth(), bmp.getHeight());

    p.setAntiAlias(true);//ww w . ja va 2  s .  c o  m
    c.drawARGB(0, 0, 0, 0);
    p.setColor(Color.BLACK);
    c.drawRoundRect(rectF, roundDP, roundDP, p);
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    c.drawBitmap(bmp, 0, 0, p);
    return bmpOut;
}

From source file:Main.java

public static Bitmap desaturateImage(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    float[] matrixs = new float[] { 1.5f, 1.5f, 1.5f, 0, -1, 1.5f, 1.5f, 1.5f, 0, -1, 1.5f, 1.5f, 1.5f, 0, -1,
            0, 0, 0, 1, 0 };//from www. jav a  2s  .c  om

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap circleBitmap(final Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w  w w.  ja v a2s.com*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    final float radius = width > height ? height / 2 : width / 2;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}