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 convertToAlphaMask(Bitmap b) {
    Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(a);
    c.drawBitmap(b, 0.0f, 0.0f, null);
    return a;/*from  w  ww . j  a  va 2 s.c  o  m*/
}

From source file:Main.java

public static Bitmap overlay(Bitmap b, int x, int y) {
    Bitmap empty = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_4444);
    Canvas cv = new Canvas(empty);
    cv.drawBitmap(b, 0, 0, null);
    cv.save();/*from w  w  w . ja v  a 2  s .  c om*/
    return empty;
}

From source file:Main.java

public static Bitmap compoundBitmap(Bitmap bg, Bitmap fg, int left, int top) {
    Bitmap result = Bitmap.createBitmap(bg.getWidth(), bg.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bg, 0, 0, null);
    canvas.drawBitmap(fg, left, top, null);

    return result;
}

From source file:Main.java

public static Bitmap convertToAlphaMask(Bitmap b) {
    Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(a);
    c.drawBitmap(b, 0.0f, 0.0f, null);

    return a;/*w  ww.j  ava2  s  .c  o  m*/
}

From source file:Main.java

public static Bitmap cropBitmap(Bitmap bitmap, Rect rect) {
    int w = rect.right - rect.left;
    int h = rect.bottom - rect.top;
    Bitmap ret = Bitmap.createBitmap(w, h, bitmap.getConfig());
    Canvas canvas = new Canvas(ret);
    canvas.drawBitmap(bitmap, -rect.left, -rect.top, null);
    bitmap.recycle();//w w  w. j  ava2s  .  co  m
    return ret;
}

From source file:Main.java

public static Bitmap setBrightness(Bitmap srcBitmap, int brightness) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0,
            brightness, 0, 0, 0, 1, 0 });
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap converttoBitmap565(Bitmap b) {
    if (b == null) {
        return null;
    }//from  w  w w  .j  a va 2s.  com
    if (b.getConfig() == Bitmap.Config.RGB_565) {
        return b;
    }
    Bitmap b565 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b565);
    c.drawBitmap(b, 0, 0, null);
    return b565;
}

From source file:Main.java

/**
 * Create a bitmap from a background and a foreground bitmap
 *
 * @param background The bitmap placed in the background
 * @param foreground The bitmap placed in the foreground
 * @return the merged bitmap/*from  w w w. j a  va2s  .  c  om*/
 */
public static Bitmap mergeBitmap(@NonNull Bitmap background, @NonNull Bitmap foreground) {
    Bitmap result = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(background, 0f, 0f, null);
    canvas.drawBitmap(foreground, 10, 10, null);
    return result;
}

From source file:Main.java

public static Bitmap getPaintedBitmap(Bitmap bitmap, Paint paint, boolean deleteOld) {
    final Bitmap colorBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    final Canvas c = new Canvas(colorBitmap);
    c.drawBitmap(bitmap, 0, 0, paint);
    if (deleteOld) {
        bitmap.recycle();/*from   w w  w  . j av  a 2s . c o  m*/
    }
    return colorBitmap;
}

From source file:Main.java

public static Bitmap changeImageColor(Bitmap sourceBitmap, int color) {
    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
            sourceBitmap.getHeight() - 1);
    Paint p = new Paint();
    p.setColor(color);/*from   www  .ja va2s  . co  m*/
    ColorFilter filter = new LightingColorFilter(color, 1);

    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, p);
    return resultBitmap;
}