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 createIdenticon(byte[] hash, int background) {
    Bitmap identicon = Bitmap.createBitmap(5, 5, Bitmap.Config.ARGB_8888);

    float[] hsv = { Integer.valueOf(hash[3] + 128).floatValue() * 360.0f / 255.0f, 0.8f, 0.8f };
    int foreground = Color.HSVToColor(hsv);

    for (int x = 0; x < 5; x++) {
        //Enforce horizontal symmetry
        int i = x < 3 ? x : 4 - x;
        for (int y = 0; y < 5; y++) {
            int pixelColor;
            //toggle pixels based on bit being on/off
            if ((hash[i] >> y & 1) == 1)
                pixelColor = foreground;
            else/*from w w  w  . ja  v  a2s. com*/
                pixelColor = background;
            identicon.setPixel(x, y, pixelColor);
        }
    }

    Bitmap bmpWithBorder = Bitmap.createBitmap(48, 48, identicon.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(background);
    identicon = Bitmap.createScaledBitmap(identicon, 46, 46, false);
    canvas.drawBitmap(identicon, 1, 1, null);

    return bmpWithBorder;
}

From source file:Main.java

private static Bitmap decorate(Bitmap bitmap, char label, int color, boolean glossy) {

    Canvas canvas = new Canvas(bitmap);

    canvas.drawColor(color);

    String labelString = String.valueOf(label);
    Paint textPaint = getTextPaint(bitmap.getHeight() * 0.75f);
    float cx = bitmap.getWidth() / 2;
    float cy = bitmap.getHeight() / 2;
    float oy = 0;

    synchronized (junkRectangle) {
        textPaint.getTextBounds(labelString, 0, 1, junkRectangle);
        oy = (junkRectangle.top + junkRectangle.bottom) / 2;
    }/*w  w w.  java  2  s  .com*/

    canvas.drawText(labelString, cx, cy - oy, textPaint);

    if (glossy) {
        canvas.drawPath(gloss(bitmap.getWidth()), getGlossPaint());
    }

    return bitmap;

}

From source file:Main.java

/** All dimensions are in pixels */
private static Bitmap createColorSwatchBitmap(int widthPixels, int heightPixels, int borderWidthPixels,
        int fillColor, int borderColor) {
    final Bitmap bitmap = Bitmap.createBitmap(widthPixels, heightPixels, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    if (borderWidthPixels > 0) {
        canvas.drawColor(borderColor);
        canvas.clipRect(borderWidthPixels, borderWidthPixels, widthPixels - borderWidthPixels,
                heightPixels - borderWidthPixels);
    }/* ww w. j ava2 s  . co m*/
    canvas.drawColor(fillColor);
    return bitmap;
}

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap//from www. j  a v  a2s .c  o m
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * Create a Bitmap that will be used as a mask to create the preview window in the background.
 * The Bitmap will be the given width and height. The Bitmap will be transparent except for a
 * gradient on the left and bottom sides.
 *
 * @param width Width of the mask./*from w w  w.j  av a 2s  . co  m*/
 * @param height Height of the mask.
 * @param gradientSize Size of the gradient.
 * @return Bitmap mask that will be used to create a preview window.
 */
private static Bitmap createPreviewWindowMask(int width, int height, int gradientSize) {

    // Do nothing if the size is invalid
    if (width <= 0 || height <= 0) {
        return null;
    }

    // Initialize the mask
    Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mask);
    canvas.drawColor(Color.TRANSPARENT);

    // If the gradient size is zero, don't draw the gradients
    if (gradientSize <= 0) {
        return mask;
    }

    // Calculate gradient rects
    Rect leftGradientRect = new Rect(0, 0, gradientSize, height - gradientSize);
    Rect bottomGradientRect = new Rect(leftGradientRect.right, height - gradientSize, width, height);
    Rect cornerGradientRect = new Rect(leftGradientRect.left, leftGradientRect.bottom, bottomGradientRect.left,
            bottomGradientRect.bottom);

    // Create left gradient
    Paint leftGradientPaint = new Paint();
    leftGradientPaint.setDither(true);
    leftGradientPaint.setShader(new LinearGradient(leftGradientRect.left, 0, leftGradientRect.right, 0,
            Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create right gradient
    Paint bottomGradientPaint = new Paint();
    bottomGradientPaint.setDither(true);
    bottomGradientPaint.setShader(
            new LinearGradient(leftGradientRect.right, bottomGradientRect.bottom, leftGradientRect.right,
                    bottomGradientRect.top, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create corner gradient
    Paint cornerGradientPaint = new Paint();
    cornerGradientPaint.setDither(true);
    cornerGradientPaint.setShader(new RadialGradient(cornerGradientRect.right, cornerGradientRect.top,
            gradientSize, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));

    // Draw the gradients
    canvas.drawRect(leftGradientRect, leftGradientPaint);
    canvas.drawRect(bottomGradientRect, bottomGradientPaint);
    canvas.drawRect(cornerGradientRect, cornerGradientPaint);

    return mask;
}

From source file:Main.java

/**
 * Save PNG image with background color//from w w  w  . j a va 2s  .c  o m
 * @param strFileName Save file path
 * @param bitmap Input bitmap
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapPNGWithBackgroundColor(String strFileName, Bitmap bitmap,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path/* ww w  .  ja  v  a  2 s. c o  m*/
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path//  ww  w  .ja v a  2 s. c  o m
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path/*from  w  ww.j av a2  s  .c o m*/
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(newBitmap, rect, rect, new Paint());
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

public static Bitmap view2Bitmap(View view) {
    if (view == null)
        return null;
    Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(ret);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null) {
        bgDrawable.draw(canvas);//from   w  w  w . ja va 2 s.  c  om
    } else {
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return ret;
}