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

/**
 * Draw input bitmap in shadow color and offset. Assumes offset is positive.
 *
 * @param inBitmap/*from   w  w w. j  a v a 2s . com*/
 * @param offsetX
 * @param offsetY
 * @param blurRadius        Not used
 * @param shadowColor
 * @return
 */
public static Bitmap shadowBitmap(Bitmap inBitmap, int offsetX, int offsetY, float blurRadius,
        int shadowColor) {

    int imgWidth = inBitmap.getWidth();
    int imgHeight = inBitmap.getHeight();

    Bitmap bottomBm = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
    Canvas bottomCanvas = new Canvas(bottomBm);
    // bottomCanvas.drawColor(shadowColor);

    Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    bottomPaint.setColor(shadowColor);
    bottomCanvas.drawRect(offsetX, offsetY, imgWidth, imgHeight, bottomPaint);

    //    MaskFilter filter = new BlurMaskFilter(Math.max(0.5f, blurRadius), BlurMaskFilter.Blur.NORMAL);
    bottomPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
    //    bottomPaint.setShadowLayer(blurRadius, offsetX, offsetX, shadowColor);
    //    bottomPaint.setMaskFilter(filter);
    bottomCanvas.drawBitmap(inBitmap, offsetX, offsetY, bottomPaint);

    return bottomBm;
}

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    canvas.drawRect(outerRect, paint);//from w w  w .  ja v a2s  .  co m

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {/*from  w w  w  .j  a  va2 s. com*/
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.rgb(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

        canvas.drawText(mText, x * scale, y * scale, paint);

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Bitmap addLabelToBitmap(Bitmap src, String label) {
    float densityFactor = Resources.getSystem().getDisplayMetrics().density;
    final float textPadding = src.getWidth() * 0.05f;

    Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);/*from w w w .ja  v  a 2 s. c om*/
    textPaint.setTextSize(12 * densityFactor);
    textPaint.setColor(Color.WHITE);
    textPaint.setStrokeWidth(2 * densityFactor);
    textPaint.setShadowLayer(1 * densityFactor, 0, 0, Color.BLACK);

    float textWidth = textPaint.measureText(label);

    float scaleFactor = (src.getWidth() - textPadding * 2) / textWidth;

    canvas.drawBitmap(src, 0, 0, textPaint);

    canvas.save();
    canvas.scale(scaleFactor, scaleFactor);
    float textPosX = (src.getWidth() / scaleFactor - textWidth) / 2;
    float textPosY = (src.getHeight() - textPadding) / scaleFactor;

    canvas.drawText(label, textPosX, textPosY, textPaint);
    canvas.restore();
    return result;
}

From source file:com.achep.acdisplay.graphics.IconFactory.java

@NonNull
private static Bitmap createEmptyIcon(@NonNull Resources res, int size) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w  w w.  ja  va  2s  .c  o  m
    paint.setColor(0xDDCCCCCC); // white gray

    final float radius = size / 2f;

    Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(icon);
    canvas.drawCircle(radius, radius, radius, paint);

    Drawable drawable = res.getDrawable(R.drawable.ic_action_warning_white);
    drawable.setBounds(0, 0, size, size);
    drawable.draw(canvas);

    return icon;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap3(Drawable imageDrawable, int radius) {

    Bitmap d = ((BitmapDrawable) imageDrawable).getBitmap();
    BitmapShader shader = new BitmapShader(d, TileMode.CLAMP, TileMode.CLAMP);

    int size = Math.min(d.getWidth(), d.getHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    //      float cornerRadius = radius;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShader(shader);/*from   w ww.  jav a  2s. co  m*/
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawCircle(outerRect.centerX(), outerRect.centerY(), d.getWidth() / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    Canvas canvas1 = new Canvas(output);

    RectF outerRect1 = new RectF(0, 0, size, size);

    Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint1.setShader(shader);
    paint1.setAntiAlias(true);
    paint1.setColor(Color.RED);
    canvas1.drawCircle(outerRect1.centerX(), outerRect1.centerY(), d.getWidth() / 2, paint);

    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    return output;
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   ww w.  ja  va 2 s . co m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    return new BitmapDrawable(res, buffer);
}

From source file:com.nextgis.maplibui.util.NotificationHelper.java

public static Bitmap getLargeIcon(int iconResourceId, Resources resources) {
    Bitmap icon = BitmapFactory.decodeResource(resources, iconResourceId);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        return icon;

    int iconSize = ControlHelper.dpToPx(40, resources);
    int innerIconSize = ControlHelper.dpToPx(24, resources);
    icon = Bitmap.createScaledBitmap(icon, iconSize, iconSize, false);
    Bitmap largeIcon = icon.copy(Bitmap.Config.ARGB_8888, true);
    icon = Bitmap.createScaledBitmap(icon, innerIconSize, innerIconSize, false);

    Canvas canvas = new Canvas(largeIcon);
    int center = canvas.getHeight() / 2;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(resources.getColor(R.color.accent));
    canvas.drawCircle(center, center, center, paint);
    paint.setColor(Color.WHITE);/*  w  w w  .  ja  va  2  s  .  c  o m*/
    canvas.drawBitmap(icon, center - icon.getWidth() / 2, center - icon.getWidth() / 2, paint);

    return largeIcon;
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w  w w .ja va2  s. co  m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);//from w w  w . java2  s .  c  o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}