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:com.linkbubble.util.Util.java

/**
 * Returns a bitmap suitable for the all apps view.
 *//*from w ww.ja  v a2s. c  om*/
static Bitmap createIconBitmap(Drawable icon, Context context) {
    synchronized (sCanvas) { // we share the statics :-(
        if (sIconWidth == -1) {
            initStatics(context);
        }

        int width = sIconWidth;
        int height = sIconHeight;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Creates and returns a bitmap by overlaying it with a specific color.
 *
 * @param bitmap//w  w w .j  av  a2  s  .  co  m
 *         The bitmap, which should be tinted, as an instance of the class {@link Bitmap}. The
 *         bitmap may not be null
 * @param color
 *         The color, which should be used for tinting, as an {@link Integer} value
 * @return The bitmap, which has been created, as an instance of the class {@link Bitmap}
 */
public static Bitmap tint(@NonNull final Bitmap bitmap, @ColorInt final int color) {
    ensureNotNull(bitmap, "The bitmap may not be null");
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(color);
    canvas.drawRect(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), paint);
    return bitmap;
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Creates and returns a bitmap from a specific color.
 *
 * @param width/*from   w ww  .ja v  a2  s  . co  m*/
 *         The width of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value. The width must be at least 1
 * @param height
 *         The height of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value. The height must be at least 1
 * @param color
 *         The color, which should be used, as an {@link Integer} value
 * @return The bitmap, which has been created, as an instance of the class {@link Bitmap}
 */
public static Bitmap colorToBitmap(final int width, final int height, @ColorInt final int color) {
    ensureAtLeast(width, 1, "The width must be at least 1");
    ensureAtLeast(height, 1, "The height must be at least 1");
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(color);
    canvas.drawRect(0, 0, width, height, paint);
    return bitmap;
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Creates and returns a bitmap from a specific text. The text is centered.
 *
 * @param context//from  ww  w. jav  a2 s  . com
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param width
 *         The width of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param height
 *         The height of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param backgroundColor
 *         The background color of the bitmap, which should be created, as an {@link Integer}
 *         value
 * @param text
 *         The text, the bitmap should be created from, as an instance of the type {@link
 *         CharSequence}. The text may neither be null, nor empty
 * @param textSize
 *         The text size, which should be used, in sp as an {@link Integer} value
 * @param textColor
 *         The text color, which should be used, as an {@link Integer} value The color of the
 *         text
 * @param typeface
 *         The typeface, which should be used, as a value of the enum {@link Typeface} or null,
 *         if the default typeface should be used
 * @return The bitmap, which has been created, as an instance of the class {@link Bitmap}
 */
public static Bitmap textToBitmap(@NonNull final Context context, final int width, final int height,
        @ColorInt final int backgroundColor, @NonNull final CharSequence text, final float textSize,
        @ColorInt final int textColor, @Nullable final Typeface typeface) {
    ensureNotNull(context, "The context may not be null");
    ensureNotNull(text, "The text may not be null");
    ensureNotEmpty(text, "The text may not be empty");
    ensureAtLeast(textSize, 1, "The text size must be at least 1");
    Bitmap bitmap = colorToBitmap(width, height, backgroundColor);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(textColor);
    paint.setTextSize(textSize * getDensity(context));
    paint.setTextAlign(Align.CENTER);

    if (typeface != null) {
        paint.setTypeface(typeface);
    }

    int x = bitmap.getWidth() / 2;
    int y = (int) ((bitmap.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    canvas.drawText(text.toString(), x, y, paint);
    return bitmap;
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Clips the corners of a bitmap in order to transform it into a round shape. Additionally, the
 * bitmap is resized to a specific size. Bitmaps, whose width and height are not equal, will be
 * clipped to a square beforehand.//from   w w w .j  av  a2 s  . com
 *
 * @param bitmap
 *         The bitmap, which should be clipped, as an instance of the class {@link Bitmap}. The
 *         bitmap may not be null
 * @param size
 *         The size, the bitmap should be resized to, as an {@link Integer} value in pixels. The
 *         size must be at least 1
 * @return The clipped bitmap as an instance of the class {@link Bitmap}
 */
public static Bitmap clipCircle(@NonNull final Bitmap bitmap, final int size) {
    Bitmap squareBitmap = clipSquare(bitmap, size);
    int squareSize = squareBitmap.getWidth();
    float radius = (float) squareSize / 2.0f;
    Bitmap clippedBitmap = Bitmap.createBitmap(squareSize, squareSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(clippedBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    canvas.drawCircle(radius, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(squareBitmap, new Rect(0, 0, squareSize, squareSize),
            new Rect(0, 0, squareSize, squareSize), paint);
    return clippedBitmap;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar.//from  w  w w .  ja v  a  2s. co  m
 *
 * @param rawBitmap bitmap to round corners
 */
public static Bitmap publishAvatar(Bitmap rawBitmap, int roundK) {
    if (rawBitmap == null)
        return null;

    try {
        int size = 0;
        if (rawBitmap.getHeight() > rawBitmap.getWidth()) {
            size = rawBitmap.getWidth();
        } else {
            size = rawBitmap.getHeight();
        }
        Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
        final RectF rectF = new RectF(rect);
        final float roundPx = roundK;

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

        rawBitmap.recycle();

        return output;
    } catch (Exception e) {

    }

    return null;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./*from ww w.  j  av a2  s. co  m*/
 *
 * @param filePath file path to avatar image
 */
public static Bitmap publishAvatar(String filePath) {
    Bitmap bitmap = null;

    if (!TextUtils.isEmpty(filePath)) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filePath, opts);
            int size = 0;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                size = bitmap.getWidth();
            } else {
                size = bitmap.getHeight();
            }
            Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
            final RectF rectF = new RectF(rect);
            final float roundPx = 12;

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

            bitmap.recycle();

            return output;
        } catch (Exception e) {

        }
    }
    return null;
}

From source file:nu.yona.app.utils.AppUtils.java

/**
 * Gets circle bitmap./*from ww  w. ja  v a  2 s  .co m*/
 *
 * @param bitmap the bitmap
 * @return the circle bitmap
 */
public 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.BLUE;
    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);
    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:de.mrapp.android.util.BitmapUtil.java

/**
 * Clips the long edge of a bitmap, if its width and height are not equal, in order to transform
 * it into a square. Additionally, the bitmap is resized to a specific size and a border will be
 * added./* w w  w  .ja v  a2s  .  c o m*/
 *
 * @param bitmap
 *         The bitmap, which should be clipped, as an instance of the class {@link Bitmap}. The
 *         bitmap may not be null
 * @param size
 *         The size, the bitmap should be resized to, as an {@link Integer} value in pixels. The
 *         size must be at least 1
 * @param borderWidth
 *         The width of the border as an {@link Integer} value in pixels. The width must be at
 *         least 0
 * @param borderColor
 *         The color of the border as an {@link Integer} value
 * @return The clipped bitmap as an instance of the class {@link Bitmap}
 */
public static Bitmap clipSquare(@NonNull final Bitmap bitmap, final int size, final int borderWidth,
        @ColorInt final int borderColor) {
    ensureAtLeast(borderWidth, 0, "The border width must be at least 0");
    Bitmap clippedBitmap = clipSquare(bitmap, size);
    Bitmap result = Bitmap.createBitmap(clippedBitmap.getWidth(), clippedBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    float offset = borderWidth / 2.0f;
    Rect src = new Rect(0, 0, clippedBitmap.getWidth(), clippedBitmap.getHeight());
    RectF dst = new RectF(offset, offset, result.getWidth() - offset, result.getHeight() - offset);
    canvas.drawBitmap(clippedBitmap, src, dst, null);

    if (borderWidth > 0 && Color.alpha(borderColor) != 0) {
        Paint paint = new Paint();
        paint.setFilterBitmap(false);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(borderWidth);
        paint.setColor(borderColor);
        offset = borderWidth / 2.0f;
        RectF bounds = new RectF(offset, offset, result.getWidth() - offset, result.getWidth() - offset);
        canvas.drawRect(bounds, paint);
    }

    return result;
}

From source file:com.almalence.googsharing.Thumbnail.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int size, int pixels) {
    final int side = Math.min(bitmap.getWidth(), bitmap.getHeight());

    final Bitmap bitmapCropped = Bitmap.createBitmap(bitmap, (bitmap.getWidth() - side) / 2,
            (bitmap.getHeight() - side) / 2, side, side);

    final Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rectSrc = new Rect(0, 0, bitmapCropped.getWidth(), bitmapCropped.getHeight());
    final Rect rect = new Rect(6, 6, output.getWidth() - 6, output.getHeight() - 6);
    final RectF rectF = new RectF(rect);
    final RectF rectFBorder = new RectF(0, 0, output.getWidth(), output.getHeight());
    final float roundPx = pixels;

    paint.setAntiAlias(true);//from  w w  w . ja v a 2s .  c om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

    paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
    canvas.drawRoundRect(rectFBorder, roundPx, roundPx, paint);

    return output;
}