Example usage for android.graphics Paint Paint

List of usage examples for android.graphics Paint Paint

Introduction

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

Prototype

public Paint() 

Source Link

Document

Create a new paint with default settings.

Usage

From source file:Main.java

/**
 * Create round, coloured bitmap with text embedded.
 * @param circleColor The color to use./*w  w w  .j  av a  2 s.c om*/
 * @param diameterDP The diameter of the circle.
 * @param text The text to embed.
 * @return Bitmap showing a text.
 */
public static Bitmap generateCircleBitmap(int circleColor, float diameterDP, String text) {
    /**
     *
     * http://stackoverflow.com/questions/31168636/rounded-quickcontactbadge-with-text
     */
    final int textColor = 0xffffffff;

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float diameterPixels = diameterDP * (metrics.densityDpi / 160f);
    float radiusPixels = diameterPixels / 2;

    // Create the bitmap
    Bitmap output = Bitmap.createBitmap((int) diameterPixels, (int) diameterPixels, Bitmap.Config.ARGB_8888);

    // Create the canvas to draw on
    Canvas canvas = new Canvas(output);
    canvas.drawARGB(0, 0, 0, 0);

    // Draw the circle
    final Paint paintC = new Paint();
    paintC.setAntiAlias(true);
    paintC.setColor(circleColor);
    canvas.drawCircle(radiusPixels, radiusPixels, radiusPixels, paintC);

    // Draw the text
    if (text != null && text.length() > 0) {
        final Paint paintT = new Paint();
        paintT.setColor(textColor);
        paintT.setAntiAlias(true);
        paintT.setTextSize(radiusPixels * 2);
        paintT.setTypeface(Typeface.SANS_SERIF);
        final Rect textBounds = new Rect();
        paintT.getTextBounds(text, 0, text.length(), textBounds);
        canvas.drawText(text, radiusPixels - textBounds.exactCenterX(),
                radiusPixels - textBounds.exactCenterY(), paintT);
    }

    return output;
}

From source file:Main.java

/**
 * Creates the Paint object for drawing the crop window border.
 * /*from  w  ww .j a v  a 2  s . c  o m*/
 * @param context the Context
 * @return new Paint object
 */
public static Paint newBorderPaint(Context context) {

    // Set the line thickness for the crop window border.
    final float lineThicknessPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            DEFAULT_LINE_THICKNESS_DP, context.getResources().getDisplayMetrics());

    final Paint borderPaint = new Paint();
    borderPaint.setColor(Color.parseColor(SEMI_TRANSPARENT));
    borderPaint.setStrokeWidth(lineThicknessPx);
    borderPaint.setStyle(Paint.Style.STROKE);

    return borderPaint;
}

From source file:Main.java

/**
 * Creates the Paint object for drawing the corners of the border
 * //from  w w w .  j  a v a  2  s.  c  o  m
 * @param context the Context
 * @return the new Paint object
 */
public static Paint newCornerPaint(Context context) {

    // Set the line thickness for the crop window border.
    final float lineThicknessPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            DEFAULT_CORNER_THICKNESS_DP, context.getResources().getDisplayMetrics());

    final Paint cornerPaint = new Paint();
    cornerPaint.setColor(DEFAULT_CORNER_COLOR);
    cornerPaint.setStrokeWidth(lineThicknessPx);
    cornerPaint.setStyle(Paint.Style.STROKE);

    return cornerPaint;
}

From source file:Main.java

/**
 * Creates a <tt>Bitmap</tt> with rounded corners.
 * @param bitmap the bitmap that will have it's corners rounded.
 * @param factor factor used to calculate corners radius based on width
 *               and height of the image.
 * @return a <tt>Bitmap</tt> with rounded corners created from given
 *         <tt>bitmap</tt>.//from  ww  w  .j a va2 s  . c o  m
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float factor) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    float rX = ((float) bitmap.getWidth()) * factor;
    float rY = ((float) bitmap.getHeight()) * factor;
    //float r = (rX+rY)/2;

    canvas.drawRoundRect(rectF, rX, rY, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2 - 5;/* w w w .  j  a  va2s.  c  o  m*/
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2 - 5;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst_left + 15, dst_top + 15, dst_right - 20, dst_bottom - 20);

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);//from  w  ww.j a  va 2s.co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;//from   w  w  w  . j  av a 2 s  .  c om
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

/**
 * Save PNG image with background color/*from w w  w  .  j a  va  2  s .com*/
 * @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

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;//ww  w.  j  a  v a 2s.com
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

/**
 * Create a bitmap which is wrapped to circle
 * (similar to what you can see in G+ profile pic.)
 *
 * @param bitmap Original Bitmap/*  w  ww . j  av a  2 s.  co  m*/
 * @return Circled bitmap
 */
public static Bitmap createCircleBitmap(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint bitmapPaint = new Paint();
    bitmapPaint.setAntiAlias(true);
    bitmapPaint.setShader(bitmapShader);

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint);

    return output;
}