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 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  v  a  2 s  .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 combineTwoBitmaps(Bitmap background, Bitmap foreground) {
    Bitmap combinedBitmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(),
            background.getConfig());/*w  w  w.  j a  v  a  2  s . c  om*/
    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

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from w  ww  .j a  v a2  s  .  co  m
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height * 2 / 3, width, height / 3, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 3), Config.ARGB_8888);

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

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

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x88ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight(), paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectImages(Bitmap bImap) {
    if (null == bImap) {
        return null;
    }// w  w w.  ja  v a2 s  .  c o  m

    final int reflectionGap = 4;
    int width = bImap.getWidth();
    int height = bImap.getHeight();

    Bitmap bitmapWithReflection = null;
    try {
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionImage = Bitmap.createBitmap(bImap, 0, height / 2, width, height / 2, matrix, false);
        bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bImap, 0, 0, null);
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bImap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    } catch (OutOfMemoryError e) {
    }

    return bitmapWithReflection;
}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap./*w  ww .  ja v a  2s . c  o m*/
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap GetBitmapClippedCircle(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Path path = new Path();
    path.addCircle((float) (width / 2), (float) (height / 2), (float) Math.min(width, (height / 2)),
            Path.Direction.CCW);//from w  w w .  j  av a 2s  . c  o m

    Canvas canvas = new Canvas(outputBitmap);
    canvas.clipPath(path);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

From source file:Main.java

private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {
    if (first == null) {
        return null;
    }/*from  www.  j  ava  2 s  .  c o m*/
    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, 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, 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, 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, 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 resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    if (bitmap == null) {
        return null;
    }/*from w ww  .  jav a2  s  .c  o  m*/
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, 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);
    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 createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*w ww .ja  va  2 s . 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

@SuppressWarnings("deprecation")
private static Drawable getSelectedDrawable(BitmapDrawable mDrawable) {
    Bitmap srcBitmap = mDrawable.getBitmap();
    Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Config.ARGB_8888);
    ColorMatrix cMatrix = new ColorMatrix();
    cMatrix.set(new float[] { 1, 0, 0, 0, -DRAW_DEEP, 0, 1, 0, 0, -DRAW_DEEP, 0, 0, 1, 0, -DRAW_DEEP, 0, 0, 0,
            1, 0 });/*w  w w  .j  a  v a  2 s .  c  o m*/
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return new BitmapDrawable(bmp);
}