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 cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
    int width = r.width();
    int height = r.height();

    Bitmap croppedImage = Bitmap.createBitmap(width, height, config);

    Canvas cvs = new Canvas(croppedImage);
    Rect dr = new Rect(0, 0, width, height);
    cvs.drawBitmap(mBitmap, r, dr, null);
    return croppedImage;
}

From source file:Main.java

/**
 * draw the image to viewBitmap in scale mode.
 * @param viewBitmap Bitmap to be displayed on the device.
 * @param bitmap Bitmap image./*from  ww  w  .ja  v a 2 s  . co m*/
 */
public static void drawImageForScalesMode(final Bitmap viewBitmap, final Bitmap bitmap) {

    float startGridX = 0;
    float startGridY = 0;

    float getSizeW = bitmap.getWidth();
    float getSizeH = bitmap.getHeight();

    float scale;
    final int width = viewBitmap.getWidth();
    final int height = viewBitmap.getHeight();
    if ((getSizeW / width) > (getSizeH / height)) {
        scale = width / getSizeW;
    } else {
        scale = height / getSizeH;
    }

    int targetW = (int) Math.ceil(scale * getSizeW);
    int targetH = (int) Math.ceil(scale * getSizeH);

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, targetW, targetH, false);

    if ((getSizeW / width) > (getSizeH / height)) {
        startGridY = (height / 2 - targetH / 2);
    } else {
        startGridX = (width / 2 - targetW / 2);
    }

    Canvas canvas = new Canvas(viewBitmap);
    canvas.drawBitmap(resizedBitmap, startGridX, startGridY, null);
}

From source file:Main.java

public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;//from ww w  .  j  a v a 2s. co  m

    int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg.getHeight();

    if (fg.getWidth() != width && fg.getHeight() != height) {
        fg = zoom(fg, width, height);
    }
    if (bgd.getWidth() != width && bgd.getHeight() != height) {
        bgd = zoom(bgd, width, height);
    }

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));

    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be//from w  w w .jav  a  2s .  c o m
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}

From source file:Main.java

private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {
    if (first == null) {
        return null;
    }//from www  .  ja  va  2  s.c om
    if (second == null) {
        return first;
    }
    int fw = first.getWidth();
    int fh = first.getHeight();
    int sw = second.getWidth();
    int sh = second.getHeight();
    Bitmap newBitmap = null;
    if (direction == LEFT) {
        newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, sw, 0, null);
        canvas.drawBitmap(second, 0, 0, null);
    } else if (direction == RIGHT) {
        newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, 0, null);
        canvas.drawBitmap(second, fw, 0, null);
    } else if (direction == TOP) {
        newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, sh, null);
        canvas.drawBitmap(second, 0, 0, null);
    } else if (direction == BOTTOM) {
        newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(first, 0, 0, null);
        canvas.drawBitmap(second, 0, fh, null);
    }
    return newBitmap;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//  w ww .jav  a 2s. c o  m

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in   
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient   
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from ww  w.  ja v a 2 s  . c  om
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from w w w.j a  va2s  .com

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap BITMAP_RESIZER(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);/*from   w w w  . j  ava2 s.c  o m*/
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

}

From source file:Main.java

public static Bitmap getReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from ww  w.ja va  2 s . com*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}