Example usage for android.graphics Canvas drawColor

List of usage examples for android.graphics Canvas drawColor

Introduction

In this page you can find the example usage for android.graphics Canvas drawColor.

Prototype

public void drawColor(@ColorInt int color) 

Source Link

Document

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.

Usage

From source file:Main.java

public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap addPadding(@NonNull Bitmap bmp) {

    int biggerParam = Math.max(bmp.getWidth(), bmp.getHeight());
    Bitmap bitmap = Bitmap.createBitmap(biggerParam, biggerParam, bmp.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);

    int top = bmp.getHeight() > bmp.getWidth() ? 0 : (bmp.getWidth() - bmp.getHeight()) / 2;
    int left = bmp.getWidth() > bmp.getHeight() ? 0 : (bmp.getHeight() - bmp.getWidth()) / 2;

    canvas.drawBitmap(bmp, left, top, null);
    return bitmap;
}

From source file:Main.java

/**
 * Helper to create a bitmap to set as imageview or bg
 * @param bgcolor/*from ww w  . j a v  a 2 s  . co m*/
 * @return bitmap
 */
public static Bitmap getBackground(int bgcolor) {
    try {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(2, 2, config);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(bgcolor);
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Drawable solidColorDrawable(int color) {
    return new Drawable() {
        @Override/*from   w w w. j  a va 2s . co m*/
        public void setColorFilter(ColorFilter cf) {
        }

        @Override
        public void setAlpha(int alpha) {
        }

        @Override
        public int getOpacity() {
            return 0;
        }

        @Override
        public void draw(Canvas canvas) {
            canvas.drawColor(0xFF000000);
        }
    };
}

From source file:Main.java

public static Bitmap addPadding(Bitmap bmp, int color) {

    if (bmp == null) {
        return null;
    }/*  www.  j  a  v a2 s  . com*/

    int biggerParam = Math.max(bmp.getWidth(), bmp.getHeight());
    Bitmap bitmap = Bitmap.createBitmap(biggerParam, biggerParam, bmp.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(color);

    int top = bmp.getHeight() > bmp.getWidth() ? 0 : (bmp.getWidth() - bmp.getHeight()) / 2;
    int left = bmp.getWidth() > bmp.getHeight() ? 0 : (bmp.getHeight() - bmp.getWidth()) / 2;

    canvas.drawBitmap(bmp, left, top, null);
    return bitmap;
}

From source file:Main.java

/**
 * Adds the white border to the specifed bitmap image using specified border size.
 * /*from w  w w  .j  a  v  a  2  s .  co  m*/
 * @param bmp to add border
 * @param borderSize
 * @return bitmap image with border.
 */
public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize, int imgSize) {

    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);

    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap getBitmap(int background, Bitmap contour, float alpha) {
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(background);
    return getBitmap(bitmap, contour, alpha);
}

From source file:Main.java

public static Bitmap getTextImage(String text, float size, int width, int height) {
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Paint paint = new Paint();
    final Canvas canvas = new Canvas(bmp);

    canvas.drawColor(Color.WHITE);

    paint.setColor(Color.BLACK);/*from  w w  w.ja va  2  s.c  o m*/
    paint.setStyle(Style.FILL);
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(size);
    paint.setTypeface(Typeface.DEFAULT);
    canvas.drawText(text, width / 2, height / 2, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap createPngScreenshot2(View view, int thumbnailWidth, int thumbnailHeight, int top) {
    if (view != null) {
        Bitmap mCapture;//w w w .j a va 2  s.  c  om
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        c.drawColor(Color.BLACK);
        //            final int left = view.getScrollX();
        //            final int top = view.getScrollY();
        c.translate(0, -top);
        //c.scale(0.65f, 0.65f, left, top);
        try {
            // draw webview may nullpoint               
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

public static byte[] bmp2bytesStream(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width % 8 != 0) {
        int adjustWidth = width + (8 - width % 8);
        final Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config);
        Canvas canvas = new Canvas(whiteBgBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);

        bitmap = whiteBgBitmap;/*  ww  w  . j av a  2s  .  c o  m*/
        width = bitmap.getWidth();
    }
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    byte[] image = new byte[width * height];
    final byte WHITE = 0, BLACK = 1;
    for (int i = 0; i < pixels.length; i++) {
        image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE;
    }
    final int COL = width + width % 2;
    byte[] row = new byte[COL];
    byte[] res = new byte[COL / 8 * height];
    for (int i = 0, dex = 0, num = 0; i < height; ++i) {
        System.arraycopy(image, i * width, row, 0, width);
        for (byte e : row) {
            res[dex] += e << (7 - num++);
            num = 8 == num ? 0 : num;
            dex = 0 == num ? dex + 1 : dex;
        }
    }
    return res;
}