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 createRoundedBottomBitmap(Context context, int width, int height, int color) {

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

    //color = 0x80424242;  // FIXME

    final Paint paint = new Paint();
    final int roundPxInt = convertDipsToPixels(context, ROUND_DIPS);

    final Rect rect = new Rect(0, 0, width, height - roundPxInt);
    final RectF rectF = new RectF(rect);
    final Rect rectRound = new Rect(roundPxInt, height - roundPxInt, width - roundPxInt, height);
    final RectF rectFRound = new RectF(rectRound);

    paint.setAntiAlias(true);//  w w w  . j a v a2 s  .c  o m
    paint.setColor(color);

    canvas.drawARGB(0, 0, 0, 0);

    // Corners
    Rect oval = new Rect(0, height - 2 * roundPxInt, 2 * roundPxInt, height);
    RectF ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 90.0f, 90.0f, true, paint);

    oval = new Rect(width - 2 * roundPxInt, height - 2 * roundPxInt, width, height);
    ovalF = new RectF(oval);
    canvas.drawArc(ovalF, 0.0f, 90.0f, true, paint);

    // Big and small rectangles
    canvas.drawRect(rectF, paint);
    canvas.drawRect(rectFRound, paint);

    return output;
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) {
    if (bitmap == null) {
        return null;
    }/*from www.j a va  2 s  .  com*/
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        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 - 5;/*from  w  w w . j av 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 - 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:com.kabootar.GlassMemeGenerator.ImageOverlay.java

/**
 * Draws the given string centered, as big as possible, on either the top or
 * bottom 20% of the image given./*www.j a  v  a  2s  .com*/
 */
private static void drawStringCentered(Canvas g, String text, Bitmap image, boolean top, Context baseContext)
        throws InterruptedException {
    if (text == null)
        text = "";

    int height = 0;
    int fontSize = MAX_FONT_SIZE;
    int maxCaptionHeight = image.getHeight() / 5;
    int maxLineWidth = image.getWidth() - SIDE_MARGIN * 2;
    String formattedString = "";
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    Paint stkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    stkPaint.setStyle(STROKE);
    stkPaint.setStrokeWidth(8);
    stkPaint.setColor(Color.BLACK);

    //Typeface tf = Typeface.create("Arial", Typeface.BOLD);
    Typeface tf = Typeface.createFromAsset(baseContext.getAssets(), "fonts/impact.ttf");

    paint.setTypeface(tf);
    stkPaint.setTypeface(tf);
    do {

        paint.setTextSize(fontSize);

        // first inject newlines into the text to wrap properly
        StringBuilder sb = new StringBuilder();
        int left = 0;
        int right = text.length() - 1;
        while (left < right) {

            String substring = text.substring(left, right + 1);
            Rect stringBounds = new Rect();
            paint.getTextBounds(substring, 0, substring.length(), stringBounds);
            while (stringBounds.width() > maxLineWidth) {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }

                // look for a space to break the line
                boolean spaceFound = false;
                for (int i = right; i > left; i--) {
                    if (text.charAt(i) == ' ') {
                        right = i - 1;
                        spaceFound = true;
                        break;
                    }
                }
                substring = text.substring(left, right + 1);
                paint.getTextBounds(substring, 0, substring.length(), stringBounds);

                // If we're down to a single word and we are still too wide,
                // the font is just too big.
                if (!spaceFound && stringBounds.width() > maxLineWidth) {
                    break;
                }
            }
            sb.append(substring).append("\n");
            left = right + 2;
            right = text.length() - 1;
        }

        formattedString = sb.toString();

        // now determine if this font size is too big for the allowed height
        height = 0;
        for (String line : formattedString.split("\n")) {
            Rect stringBounds = new Rect();
            paint.getTextBounds(line, 0, line.length(), stringBounds);
            height += stringBounds.height();
        }
        fontSize--;
    } while (height > maxCaptionHeight);

    // draw the string one line at a time
    int y = 0;
    if (top) {
        y = TOP_MARGIN;
    } else {
        y = image.getHeight() - height - BOTTOM_MARGIN;
    }
    for (String line : formattedString.split("\n")) {
        // Draw each string twice for a shadow effect
        Rect stringBounds = new Rect();
        paint.getTextBounds(line, 0, line.length(), stringBounds);
        //paint.setColor(Color.BLACK);
        //g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2 + 2, y + stringBounds.height() + 2, paint);

        paint.setColor(Color.WHITE);
        g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2, y + stringBounds.height(), paint);

        //stroke
        Rect strokeBounds = new Rect();
        stkPaint.setTextSize(fontSize);
        stkPaint.getTextBounds(line, 0, line.length(), strokeBounds);
        g.drawText(line, (image.getWidth() - (int) strokeBounds.width()) / 2, y + strokeBounds.height(),
                stkPaint);

        y += stringBounds.height();
    }
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap addShadow(Bitmap src, float radius, float dx, float dy, int shadowColor) {

    int margin = 0;
    if (radius <= 0)
        radius = 1;// w ww  . ja v  a  2  s . co  m
    if (dx < 0)
        dx = 0;
    if (dy < 0)
        dy = 0;

    margin = (int) radius;

    Bitmap alpha = src.extractAlpha();

    Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin * 2, src.getHeight() + margin * 2,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bmp);
    //LogHelper.log ("Canvas.isHardwareAccelerated() " + canvas.isHardwareAccelerated() );
    Paint paint = new Paint();
    paint.setColor(shadowColor);

    paint.setShadowLayer(radius, dx, dy, shadowColor);

    canvas.drawBitmap(alpha, margin, margin, paint);
    canvas.drawBitmap(src, margin, margin, null);

    return bmp;
}

From source file:ar.uba.fi.splitapp.MockServer.java

private static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    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);/*from   w  w  w.  ja va  2  s  .  c o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap addGlow(Bitmap src, int glowColor, float radius) {
    float DEFAULT_GLOW_RADIUS_FACTOR = 0.1f;
    // An added margin to the initial image
    int margin;//from   w w  w. jav  a2s. c o  m
    int glowRadius;

    int midSize = (src.getWidth() + src.getHeight()) / 2;

    if (radius > 0 && radius < 1)
        glowRadius = (int) (midSize * radius);
    else if (radius >= 1)
        glowRadius = (int) radius;
    else
        glowRadius = (int) (midSize * DEFAULT_GLOW_RADIUS_FACTOR);

    margin = (int) (glowRadius);

    Bitmap alpha = src.extractAlpha();

    // The output bitmap (with the icon + glow)
    Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin * 2, src.getHeight() + margin * 2,
            Bitmap.Config.ARGB_8888);

    //LogHelper.log("glow new size : " + (src.getWidth() + margin*2));
    // The canvas to paint on the image
    Canvas canvas = new Canvas(bmp);

    Paint paint = new Paint();
    paint.setColor(glowColor);

    // outer glow
    paint.setMaskFilter(new BlurMaskFilter(glowRadius, BlurMaskFilter.Blur.OUTER));
    canvas.drawBitmap(alpha, margin, margin, paint);

    // original icon
    canvas.drawBitmap(src, margin, margin, null);

    return bmp;
}

From source file:eu.iescities.pilot.rovereto.roveretoexplorer.map.MapManager.java

private static void drawPath(GoogleMap map, List<LatLng> points, int color) {
    // int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(6);// www.j av  a  2 s  .co m

    PolylineOptions po = new PolylineOptions().addAll(points).width(6).color(color);
    Polyline pl = map.addPolyline(po);
    pl.setVisible(true);
}

From source file:com.chalmers.feedlr.adapter.FeedAdapter.java

/**
 * // w w w .  java2 s  . c  om
 * @param squareBitmap
 *            original image
 * @return image with rounded corners
 */
public static Bitmap getRoundedCornerBitmap(Bitmap squareBitmap) {
    Bitmap roundedBitmap = Bitmap.createBitmap(squareBitmap.getWidth(), squareBitmap.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);

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

    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(squareBitmap, rect, rect, paint);

    return roundedBitmap;
}

From source file:org.cocos2dx.lib.Cocos2dxBitmap.java

private static Paint newPaint(String fontName, int fontSize, int alignment) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setTextSize(fontSize);//www .  ja va2s  .  c  om
    paint.setAntiAlias(true);

    /*
     * Set type face for paint, now it support .ttf file.
     */
    if (fontName.endsWith(".ttf")) {
        try {
            //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName);
            Typeface typeFace = Cocos2dxTypefaces.get(context, fontName);
            paint.setTypeface(typeFace);
        } catch (Exception e) {
            Log.e("Cocos2dxBitmap", "error to create ttf type face: " + fontName);

            /*
             * The file may not find, use system font
             */
            paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
        }
    } else {
        paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
    }

    int hAlignment = alignment & 0x0F;
    switch (hAlignment) {
    case HALIGNCENTER:
        paint.setTextAlign(Align.CENTER);
        break;

    case HALIGNLEFT:
        paint.setTextAlign(Align.LEFT);
        break;

    case HALIGNRIGHT:
        paint.setTextAlign(Align.RIGHT);
        break;

    default:
        paint.setTextAlign(Align.LEFT);
        break;
    }

    return paint;
}