Example usage for android.graphics Paint setColor

List of usage examples for android.graphics Paint setColor

Introduction

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

Prototype

public void setColor(@ColorInt int color) 

Source Link

Document

Set the paint's color.

Usage

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;/*from   w w w  .jav  a 2  s  . 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;
        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);
    if (bitmap != null && !bitmap.isRecycled()) {
        bitmap.recycle();
    }
    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;/*from   w w  w  .ja v a2 s.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;
        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

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    bitmapResult = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0 + 5, 0 + 5, w - 5, h - 5);
    final RectF rectF = new RectF(rect);

    // make sure that our rounded corner is scaled appropriately

    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);/* w  ww  . j ava  2s  . c o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawColor(Color.BLACK);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    // draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    // paint.setXfermode(new AvoidXfermode(Color.WHITE, 255,
    // AvoidXfermode.Mode.TARGET));
    canvas.drawBitmap(input, 0, 0, paint);

    return bitmapResult;
}

From source file:com.android.mail.ui.FolderDisplayer.java

public static void drawFolder(Canvas canvas, float x, float y, int width, int height, String name, int fgColor,
        int bgColor, FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter, Paint paint) {
    canvas.save();/*from  w  w  w.j a  v a  2  s  .com*/
    canvas.translate(x, y + res.folderVerticalOffset);

    // Draw the box.
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.FILL);
    final RectF rect = new RectF(0, 0, width, height);
    canvas.drawRoundRect(rect, res.folderRoundedCornerRadius, res.folderRoundedCornerRadius, paint);

    // Draw the text based on the language locale and layout direction.
    paint.setColor(fgColor);
    paint.setStyle(Paint.Style.FILL);

    // Compute the text/gradient indices
    final int textLength = (int) paint.measureText(name);
    final int gradientX0;
    final int gradientX1;
    final int textX;

    /***************************************************************************************************
     * width               - the actual folder chip rectangle.                                         *
     * textLength          - the length of the folder's full name (can be longer than                  *
     *                         the actual chip, which is what overflow gradient is for).               *
     * innerPadding        - the padding between the text and the chip edge.                           *
     * overflowPadding     - the padding between start of overflow and the chip edge.                  *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a RTL language                                                                       *
     *                                                                                                 *
     *                   index-0                                                                       *
     *                      |<---------------------------- width ---------------------------->|        *
     *        |<-------------------------textLength------------------>|                       |        *
     *        |             |<----- overflowPadding ----->|                                   |        *
     *        |             |<- innerPadding ->|<-------->|<--------->|<- horizontalPadding ->|        *
     *       textX                            gX1        gX0                                           *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a LTR language.                                                                      *
     *                                                                                                 *
     *     index-0                                                                                     *
     *        |<------------------------------ width ------------------------------->|                 *
     *        |                       |<-------------------------textLength-------------------->|      *
     *        |                                   |<-------- overflowPadding ------->|                 *
     *        |<- horizontalPadding ->|<--------->|<-------->|<- horizontalPadding ->|                 *
     *                              textX        gX0        gX1                                        *
     *                                                                                                 *
     **************************************************************************************************/
    if (formatter.isRtl(name)) {
        gradientX0 = res.overflowGradientPadding;
        gradientX1 = res.folderHorizontalPadding;
        textX = width - res.folderHorizontalPadding - textLength;
    } else {
        gradientX0 = width - res.overflowGradientPadding;
        gradientX1 = width - res.folderHorizontalPadding;
        textX = res.folderHorizontalPadding;
    }

    // Draw the text and the possible overflow gradient
    // Overflow happens when the text is longer than the chip width minus side paddings.
    if (textLength > width - 2 * res.folderHorizontalPadding) {
        final Shader shader = new LinearGradient(gradientX0, 0, gradientX1, 0, fgColor,
                Utils.getTransparentColor(fgColor), Shader.TileMode.CLAMP);
        paint.setShader(shader);
    }
    final int textY = height / 2 - (int) (paint.descent() + paint.ascent()) / 2;
    canvas.drawText(name, textX, textY, paint);
    paint.setShader(null);

    canvas.restore();
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from  w w  w  .  j av a 2s.  c  o  m
    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;
        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);

    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }
    return output;
}

From source file:Main.java

public static Bitmap get_circular_bitmap(Bitmap bitmap) {
    Bitmap output;// www .  java2  s. c o m

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, 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) {
    if (bitmap == null) {
        return null;
    }//from w  w w.j  a v a2s. co m
    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;
        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

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;/*ww w  .  ja v a2  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 = null;
    try {
        output = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    } catch (Exception e) {
        return output;
    }
    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

public static Bitmap toRoundBitmap(Bitmap bitmap, int round) {
    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) {
        if (round != 0)
            roundPx = round;//from  www  .  j  a  v a 2  s . c o m
        else
            roundPx = width / 2;
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        if (round != 0)
            roundPx = round;
        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

public static Bitmap getRoundBitmap(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.ja  v a 2s  .  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;
}