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

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

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

    Canvas canvas = new Canvas(viewBitmap);

    final int width = viewBitmap.getWidth();
    final int height = viewBitmap.getHeight();
    for (int drawY = 0; drawY <= height; drawY += getSizeH) {
        for (int drawX = 0; drawX <= width; drawX += getSizeW) {
            canvas.drawBitmap(bitmap, drawX, drawY, null);
        }
    }
}

From source file:Main.java

/**
 * Use touch-icon or higher-resolution favicon and round the corners.
 * @param context    Context used to get resources.
 * @param touchIcon  Touch icon bitmap.//  w w w  .  ja v a2 s.c  o m
 * @param canvas     Canvas that holds the touch icon.
 */
private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);
    // Convert dp to px.
    int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOUCHICON_BORDER_RADII,
            context.getResources().getDisplayMetrics());
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
    path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);/*from   w  w  w.  j a  v  a2s. c om*/
    iconPaint.setFilterBitmap(true);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}

From source file:Main.java

public static final Bitmap complementedBitmapByColor(Bitmap bitmap, int color) {
    if (null == bitmap) {
        return null;
    }//from w  ww  .  j a  v a 2 s .  co m

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height) {
        return bitmap;
    }

    int len = width > height ? width : height;
    Bitmap output = Bitmap.createBitmap(len, len, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // paint.setAlpha(0);
    paint.setColor(color);
    canvas.drawPaint(paint);

    if (width > height) {
        int devide = (width - height) / 2;
        canvas.drawBitmap(bitmap, 0, devide, paint_comm);
    } else {
        int devide = (height - width) / 2;
        canvas.drawBitmap(bitmap, devide, 0, paint_comm);
    }
    return output;
}

From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java

public static Bitmap overlay(Bitmap image, String topCaption, String bottomCaption, Context baseContext)
        throws IOException, InterruptedException {

    Canvas graphics = new Canvas(image);
    graphics.drawBitmap(image, 0, 0, null);
    drawStringCentered(graphics, topCaption, image, true, baseContext);
    graphics.drawBitmap(image, 0, 0, null);
    drawStringCentered(graphics, bottomCaption, image, false, baseContext);
    return image;
}

From source file:Main.java

public static final Bitmap grey(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap greyBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(greyBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);/* w  w  w  .  j  a  v a  2s.c om*/
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return greyBitmap;
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);/* w  ww.jav  a  2 s  . c o m*/
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }
}

From source file:Main.java

private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());

    // Paint used for scaling the bitmap and drawing the rounded rect.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);/*from   w w  w .j a  v  a 2  s . c om*/
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);

    // Construct a path from a round rect. This will allow drawing with
    // an inverse fill so we can punch a hole using the round rect.
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(1, 1);
    path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);

    // Reuse the paint and clear the outside of the rectangle.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

public static Bitmap getSaturationBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap faceIconGreyBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(faceIconGreyBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);//w w w  .ja v  a2 s.com
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return faceIconGreyBitmap;
}

From source file:Main.java

/**
 * Crop bitmap image into a round shape/*from  w  w  w  .j  a v a 2  s .c  o m*/
 *
 * @param bitmap   the bitmap
 * @param diameter the diameter of the resulting image
 * @return the rounded bitmap
 */
private static Bitmap getRoundedShape(@NonNull Bitmap bitmap, int diameter) {

    Bitmap resultBitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(resultBitmap);
    Path path = new Path();
    path.addCircle(((float) diameter - 1) / 2, ((float) diameter - 1) / 2, (((float) diameter) / 2),
            Path.Direction.CCW);

    canvas.clipPath(path);
    resultBitmap.setHasAlpha(true);
    canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
            new Rect(0, 0, diameter, diameter), null);
    return resultBitmap;
}