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 setShadow(Bitmap bitmap, int radius) {
    BlurMaskFilter blurFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER);
    Paint shadowPaint = new Paint();
    shadowPaint.setAlpha(50);/* w ww .jav a2s  .  co  m*/
    shadowPaint.setColor(0xff424242);
    shadowPaint.setMaskFilter(blurFilter);
    int[] offsetXY = new int[2];
    Bitmap shadowBitmap = bitmap.extractAlpha(shadowPaint, offsetXY);
    Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(shadowImage32);
    c.drawBitmap(bitmap, -offsetXY[0], -offsetXY[1], null);
    return shadowImage32;
}

From source file:Main.java

public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) {
    final int w = src.getWidth();
    final int h = src.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(src, 0, 0, null);
    if (direction == LEFT_TOP) {
        canvas.drawBitmap(watermark, spacing, spacing, null);
    } else if (direction == LEFT_BOTTOM) {
        canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null);
    } else if (direction == RIGHT_TOP) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null);
    } else if (direction == RIGHT_BOTTOM) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing,
                null);// w  w w.  j ava2 s  . c om
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    int i = bitmap.getWidth();
    int j = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1.0F, -1F);//from  ww w  .j av a  2s .  c o  m
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, j / 2, i, j / 2, matrix, false);
    Bitmap bitmap2 = Bitmap.createBitmap(i, j + j / 2, android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap2);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, null);
    Paint paint = new Paint();
    canvas.drawRect(0.0F, j, i, j + 4, paint);
    canvas.drawBitmap(bitmap1, 0.0F, j + 4, null);
    Paint paint1 = new Paint();
    paint1.setShader(new LinearGradient(0.0F, bitmap.getHeight(), 0.0F, 4 + bitmap2.getHeight(), 0x70ffffff,
            0xffffff, android.graphics.Shader.TileMode.CLAMP));
    paint1.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
    canvas.drawRect(0.0F, j, i, 4 + bitmap2.getHeight(), paint1);
    return bitmap2;
}

From source file:Main.java

/**
 * Method to combine two images side by side.
 *
 * @param leftBmp  The left Bitmap./*from  w  w w .j a  v a 2s.com*/
 * @param rightBmp The right Bitmap.
 * @return A Bitmap with left and right bitmap are glued side by side.
 */
public static Bitmap combineTwoImagesSideBySide(Bitmap leftBmp, Bitmap rightBmp) {
    int width;
    int height = leftBmp.getHeight();

    if (leftBmp.getWidth() > rightBmp.getWidth()) {
        width = leftBmp.getWidth() + rightBmp.getWidth();
    } else {
        width = rightBmp.getWidth() + rightBmp.getWidth();
    }

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

    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(leftBmp, 0f, 0f, null);
    comboImage.drawBitmap(rightBmp, leftBmp.getWidth(), 0f, null);

    return cs;
}

From source file:Main.java

/**
 * Makes a texture from any bitmap. /*from   w  ww.j  av a2  s  . co  m*/
 * (Texture should have size that is power of 2) 
 * @param bmp bitmap
 * @return BitmapDrawable that has size that is power of 2. Bitmap is not stretched, free space 
 * is filled with default color.
 */
public static Bitmap makeTexture(Resources res, Bitmap bmp) {
    if (bmp == null) {
        throw new IllegalArgumentException("Bitmap can't be null");
    }

    int height = (int) roundPower2(bmp.getHeight());
    int width = (int) roundPower2(bmp.getWidth());

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

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bmp, 0, 0, null);

    return result;
}

From source file:Main.java

public static Bitmap combineImages(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;//w w  w .j  a  v a 2  s . com

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

    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 combineImages(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;/*from  w ww . j a v  a  2  s .c  o  m*/

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

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.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

/**
 * Adds tint effect to target bitmap.// w  w w. j  a v  a2  s . com
 *
 * @param src
 *            PNG bitmap to apply tint affect
 * @param argb
 *            tint color
 * @return bitmap with tint affect
 */
public static Bitmap addTintEffect(Bitmap src, int argb) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(argb, PorterDuff.Mode.SRC_OVER));

    Canvas bottomCanvas = new Canvas(src);
    bottomCanvas.drawBitmap(src, 0, 0, paint);

    return src;
}

From source file:Main.java

public static Bitmap mergeBitmap(Bitmap oriBmp, Bitmap subBmp, final Rect oriRect, final Rect subRect) {
    if (subBmp == null) {
        return oriBmp;
    }//w w  w  .  j  av  a  2  s. co  m

    if (oriBmp == null) {
        return null;
    }

    if (!oriBmp.isMutable()) {
        oriBmp = createMutableBitmap(oriBmp);
    }

    Canvas canvas = new Canvas(oriBmp);
    canvas.drawBitmap(subBmp, subRect, oriRect, null);
    return oriBmp;
}

From source file:Main.java

public static Bitmap getColoredBitmap(Bitmap colorBitmap, int color) {
    Bitmap grayscaleBitmap = toGrayscale(colorBitmap);
    Paint pp = new Paint();
    PorterDuffColorFilter frontFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY);
    pp.setColorFilter(frontFilter);//from   w w w . j a  va 2 s  .com
    Canvas cc = new Canvas(grayscaleBitmap);
    cc.drawBitmap(grayscaleBitmap, 0, 0, pp);
    return grayscaleBitmap;
}