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 cropWithCanvas(Bitmap bitmap, Rect cropRect) {
    Rect destRect = new Rect(0, 0, cropRect.width(), cropRect.height());
    Bitmap cropped = Bitmap.createBitmap(cropRect.width(), cropRect.height(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, cropRect, destRect, null);
    return cropped;
}

From source file:Main.java

public static Bitmap tintBitmap(Bitmap src, int color) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dst);
    canvas.drawBitmap(src, 0, 0, paint);
    return dst;//from  ww  w. java  2s .c  o m
}

From source file:Main.java

public static Bitmap setSaturation(Bitmap srcBitmap, float sat) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(sat);/* w  ww.  java  2  s.  c  om*/
    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 changeBitmapColor(@NonNull Bitmap sourceBitmap, @IntegerRes int color) {

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
            sourceBitmap.getHeight() - 1);
    Paint p = new Paint();
    ColorFilter filter = new LightingColorFilter(color, 1);
    p.setColorFilter(filter);//w  ww  .j a v a  2 s .c om

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

From source file:Main.java

public static Bitmap setupFrame(Bitmap bitmap, int width, int color) {
    if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) {
        return bitmap;
    }//from  www .  j  av a  2  s.  c  om

    Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    Canvas canvas = new Canvas(bp);
    canvas.drawBitmap(bitmap, 0, 0, new Paint());
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStrokeWidth(width);
    paint.setStyle(Style.STROKE);
    canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint);

    bitmap.recycle();
    return bp;
}

From source file:Main.java

public static Bitmap setContrast(Bitmap srcBitmap, float contrast) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(
            new float[] { contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 });
    cm.setSaturation(contrast);//from   w w w.j  a v a2s  . c  om
    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

/**
 * Draw the image to viewBitmap at the same scale drawing mode.
 * @param viewBitmap Bitmap to be displayed on the device.
 * @param bitmap Bitmap image.//from   w w  w . ja  v a 2 s  .co m
 * @param x x
 * @param y y
 */
public static void drawImageForNonScalesMode(final Bitmap viewBitmap, final Bitmap bitmap, final double x,
        final double y) {

    float startGridX = (float) x;
    float startGridY = (float) y;

    Canvas canvas = new Canvas(viewBitmap);

    canvas.drawBitmap(bitmap, startGridX, startGridY, null);
}

From source file:Main.java

public static Bitmap scale(Bitmap b, float scale) {
    Bitmap res = Bitmap.createBitmap((int) (b.getWidth() * scale + .5f), (int) (b.getHeight() * scale + .5f),
            b.getConfig());//from  www . j a  v a2  s. c om
    Canvas c = new Canvas(res);
    c.scale(scale, scale);
    c.drawBitmap(b, 0, 0, scalePaint);
    return res;
}

From source file:Main.java

public static Bitmap combineDrawables(Resources resources, int head, int body, int legs) {
    Bitmap headBitmap = getBitmap(resources, head);
    Bitmap bodyBitmap = getBitmap(resources, body);
    Bitmap legsBitmap = getBitmap(resources, legs);

    int height = headBitmap.getHeight() + bodyBitmap.getHeight() + legsBitmap.getHeight();
    int width = Math.max(headBitmap.getWidth(), Math.max(bodyBitmap.getWidth(), legsBitmap.getWidth()));

    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(result);
    comboImage.drawBitmap(headBitmap, 0f, 0f, null);
    comboImage.drawBitmap(bodyBitmap, 0f, headBitmap.getHeight(), null);
    comboImage.drawBitmap(legsBitmap, 0f, headBitmap.getHeight() + bodyBitmap.getHeight(), null);

    return result;
}

From source file:Main.java

/**
 * Method to remove colors in a Bitmap, creating a gray scale image.
 *
 * @param source The original Bitmap.//from   w  w w.  j  av  a  2  s  . c om
 * @return The gray scale Bitmap.
 */
public static Bitmap toGrayscale(Bitmap source) {
    Bitmap bmpGrayscale = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpGrayscale);
    canvas.drawBitmap(source, 0, 0, getGrayScalePaint());
    return bmpGrayscale;
}