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 final Bitmap grey(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);/* ww  w .  j a v a 2  s  .co  m*/
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return faceIconGreyBitmap;
}

From source file:Main.java

public static Bitmap toOvalBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   ww w . j a v  a 2s.  c om*/
    Rect rect = new Rect(0, 0, bitmap.getHeight(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rectF, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap out = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(out);
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);//from  w  ww  .  j  av a 2 s  .  c  o  m
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect rect = new Rect(0, 0, width, height);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return out;
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 * //from w  ww. j av a 2  s  .  c o m
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *            be cropped.
 * @param size The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 *         shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    Bitmap blurbitmap = null;

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;
        blurbitmap = BitmapFactory.decodeStream(bs, null, options);
        options.inJustDecodeBounds = false;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bs != null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * blurbitmap.getWidth());
    final int mHeight = Math.round(mScale * blurbitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;

}

From source file:Main.java

public static Bitmap roundCorners(final Bitmap bitmap, final int radiusX, final int radiusY) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w  w  w  . j a  v  a 2s . c  om
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radiusX, radiusY, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    return rounded;
}

From source file:com.wareninja.opensource.gravatar4android.common.Utils.java

public static Bitmap getBitmapWithReflection(Bitmap originalImage) {

    //The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    //This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from   ww  w  .ja va  2 s .  co  m*/

    //Create a Bitmap with the flip matrix applied to it.
    //We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix,
            false);

    //Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    //Create a new Canvas with the bitmap that's big enough for
    //the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    //Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    //Draw in the gap
    Paint deafaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
    //Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    //Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    //Set the paint to use this shader (linear gradient)
    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 roundCorners(final Bitmap source, final float radius) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);//w w w .ja v a2s  .  c om
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

/**
 * Method to remove color in a Bitmap, creating a gray scale image.
 *
 * @param bmpOriginal The original Bitmap.
 * @return The gray scale Bitmap.// w ww.  j a va2 s. com
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    Bitmap bmpGrayscale = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);
    canvas.drawBitmap(bmpOriginal, 0, 0, paint);

    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap getRoundedCircleBitmap(Bitmap scaleBitmapImage) {
    int targetWidth = 150;
    int targetHeight = 150;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);/*from w ww  .  j a v a  2s . c  o  m*/
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap revertImage(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    float[] matrixs = new float[] { -1, 0, 0, 1, 1, 0, -1, 0, 1, 1, 0, 0, -1, 1, 1, 0, 0, 0, 1, 0 };

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);//from   w ww .j a  v a 2 s  .  c o  m
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}