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 addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap cropJpgFile(String inFl, String outFl) throws IOException {
    Bitmap bmpIn = BitmapFactory.decodeFile(inFl);
    Bitmap bmOverlay = Bitmap.createBitmap(bmpIn.getWidth(), bmpIn.getHeight(), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Canvas c = new Canvas(bmOverlay);
    c.drawBitmap(bmpIn, 0, 0, null);
    //c.drawRect(30, 30, 100, 100, p);
    File fileOut = new File(outFl);
    FileOutputStream out = new FileOutputStream(fileOut);
    bmOverlay = drawTextToBitmap(bmOverlay, "Image Viewer");
    bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, out);
    return bmOverlay;
}

From source file:Main.java

/**
 * Method to combine images side by side.
 *
 * @param leftBmp  The left Bitmap./*from  w ww.  ja  v a  2 s. c o  m*/
 * @param rightBmp The right Bitmap.
 * @return A Bitmap with left and right bitmap are glued side by side.
 */
public static Bitmap combineImagesSideBySide(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

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScaleType scalingLogic) {/*w w  w .  ja v a2  s. c om*/
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
    return scaledBitmap;
}

From source file:Main.java

public static Bitmap punchAHoleInABitmap(Context context, Bitmap foreground, float x1, float y1) {
    Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(foreground, 0, 0, paint);
    paint.setAntiAlias(true);//from   w  w w. j  a  va2 s.  c  o  m
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    float radius = (float) (getScreenSize(context).x * .06);
    canvas.drawCircle(x1, y1 - 450, radius, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap GetBitmapClippedCircle(Bitmap bitmap) {

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    final Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Path path = new Path();
    path.addCircle((float) (width / 2), (float) (height / 2), (float) Math.min(width, (height / 2)),
            Path.Direction.CCW);/* w ww  .  j  ava 2 s  .c  om*/

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

From source file:Main.java

public static Bitmap setReflection(Bitmap bitmap, int distance, float ratio) {
    final int reflectionGap = distance;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from  w w w  .j av  a2s.  c  o m
    Bitmap reflectionBitmap = Bitmap.createBitmap(bitmap, 0, height / 2, width, (int) (height * ratio), matrix,
            false);
    Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height + (int) (height * ratio) + reflectionGap),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(withReflectionBitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    defaultPaint.setColor(Color.TRANSPARENT);
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, withReflectionBitmap.getHeight(),
            0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);
    return withReflectionBitmap;
}

From source file:Main.java

static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{
    if (sourceBitmap != null) {
        float R = Color.red(rgbcolor);
        float G = Color.green(rgbcolor);
        float B = Color.blue(rgbcolor);

        Log.v("R:G:B", R + ":" + G + ":" + B); //

        float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color
                0, G / 255f, 0, 0, 0 // G color
                , 0, 0, B / 255f, 0, 0 // B color
                , 0, 0, 0, 1f, 0f };//from w  w  w.  j  ava2 s  .  c o  m

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); // Remove Colour

        colorMatrix.set(colorTransform);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);

        Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        view.setImageBitmap(mutableBitmap);
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    }
}

From source file:Main.java

/**
 * Adds the white border to the specifed bitmap image using specified border size.
 * /* w  w  w. j av a2s.  com*/
 * @param bmp to add border
 * @param borderSize
 * @return bitmap image with border.
 */
public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize, int imgSize) {

    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);

    return bmpWithBorder;
}

From source file:Main.java

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

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from  w  w  w .  ja  v a2 s  . co  m

    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 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(originalImage, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

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

    return bitmapWithReflection;
}