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 int2Icon(Context context, int i) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 0x7f020047);
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap1);
    Paint paint = new Paint();
    paint.setDither(true);//  www .  j av  a  2 s  .c o  m
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
    bitmap.recycle();
    Paint paint1 = new Paint(257);
    paint1.setColor(-1);
    paint1.setTypeface(Typeface.DEFAULT_BOLD);
    paint1.setTextAlign(android.graphics.Paint.Align.CENTER);
    canvas.drawText(String.valueOf(i), bitmap.getWidth() / 2, 3 + bitmap.getHeight() / 2, paint1);
    return bitmap1;
}

From source file:Main.java

public static Bitmap duplicateBitmap(Bitmap bmpSrc) {
    if (null == bmpSrc) {
        return null;
    }/*w  w  w  . j ava 2  s  .com*/

    int bmpSrcWidth = bmpSrc.getWidth();
    int bmpSrcHeight = bmpSrc.getHeight();

    Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888);
    if (null != bmpDest) {
        Canvas canvas = new Canvas(bmpDest);
        final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);

        canvas.drawBitmap(bmpSrc, rect, rect, null);
    }

    return bmpDest;
}

From source file:Main.java

public static Bitmap createReflectedImage(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   w  w  w .  j  ava  2 s .com*/

    // 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 defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    // 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

/**
 * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap,
 * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside.
 *
 * @return//from  www .j a va  2 s.  c o  m
 */
public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) {
    Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
    clearBitmap.eraseColor(color);

    Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor);

    int frameWidth = clearBitmap.getWidth();
    int frameHeight = clearBitmap.getHeight();
    int imageWidth = resizedInsideBitmap.getWidth();
    int imageHeight = resizedInsideBitmap.getHeight();

    Canvas canvas = new Canvas(clearBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2,
            paint);
    return clearBitmap;
}

From source file:Main.java

/**
 * Method to overlay a color on a gray scale Bitmap, passed as a resource id {@code id}.
 * If you want to call this method from a Fragment/Activity call it in this way:
 * overlayColorOnGrayScale(getResources(), R.drawable.your_image, Color.RED)
 *
 * @param res   A reference to Resources.
 * @param id    The id of a drawable image.
 * @param color Color to overlay./*  www .  ja v a2 s .co m*/
 * @return A colored gray scale Bitmap.
 * @throws IOException
 */
public static Bitmap overlayColorOnGrayScale(Resources res, int id, int color) throws IOException {
    Bitmap mutableBitmap = getMutableBitmap(res, id);

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawBitmap(mutableBitmap, 0, 0, getGrayScalePaint());

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    return mutableBitmap;
}

From source file:Main.java

private static Bitmap buildBitmap(ArrayList<Bitmap> bitmaps) {
    if (bitmaps == null || bitmaps.size() == 0) {
        return null;
    }//from w w w. j  a  v a  2 s.c  o m
    int width = 0;
    int height = 0;
    for (int i = 0; i < bitmaps.size(); i++) {
        width = width + bitmaps.get(i).getWidth();
        height = Math.max(height, bitmaps.get(i).getHeight());
    }
    Bitmap resultBitmap = Bitmap.createBitmap(width, height, Config.ARGB_4444);
    int drawWidth = 0;
    Canvas canvas = new Canvas(resultBitmap);
    for (int j = 0; j < bitmaps.size(); j++) {
        drawWidth = j * bitmaps.get(j).getWidth();
        canvas.drawBitmap(bitmaps.get(j), drawWidth, 0, null);
    }
    return resultBitmap;
}

From source file:Main.java

/**
 * ---------------------------------------------------------------------------
 * USE THIS METHOD AND NOT THE OLDER VERSION CALLED: "overlayColorOnGrayScale"
 * ---------------------------------------------------------------------------
 * Method to overlay color on a gray scale Bitmap.
 * This method creates automatically a gray scale bitmap from {@code source}.
 *
 * @param source The original colored Bitmap.
 * @param color  Color to overlay./*from  w  w  w.ja  v  a2s  .c  om*/
 * @return A colored gray scale Bitmap.
 */
public static Bitmap overlayColorOnGrayScale(Bitmap source, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawBitmap(source, 0, 0, getGrayScalePaint());

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    return mutableBitmap;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*from  w  ww. ja v a  2s  . c  o  m*/

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 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),
                Bitmap.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(bitmap, 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, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
                Shader.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(PorterDuff.Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
    int width = Math.round(bitmap.getWidth() * scale);
    int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);/*from   ww  w  . ja v  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;
}

From source file:Main.java

public static Bitmap toGreyBitmap(Bitmap bitmap) {
    Bitmap grey = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    Paint p = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);//from  www. ja v a  2s  . c  o  m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}