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 drawTextToBitmap(Bitmap bitmap, String gText) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from  ww w. j av a  2 s  .c o m
    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(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}

From source file:Main.java

public static void drawBackground(Canvas canvas, int backgroundColor, int width, int height) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);//from   ww w .  j a  va 2s  .co  m
    paint.setColor(backgroundColor);
    canvas.drawRect(0, 0, width, height, paint);
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12,
            Bitmap.Config.ARGB_8888);/*from   w w  w  .  j a  v a 2s  .co m*/

    Canvas canvas = new Canvas(output);
    //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor));

    Paint borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    borderPaint.setColor(borderColor);
    borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK);

    int centerWidth = output.getWidth() / 2;
    int centerHeight = output.getHeight() / 2;
    canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12);
    Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight());

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, rectS, rectD, paint);

    return output;
}

From source file:Main.java

public static BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*  ww w  .j  a v  a 2  s  .  com*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:Main.java

public static final Bitmap complementedBitmapByColor(Bitmap bitmap, int color) {
    if (null == bitmap) {
        return null;
    }//from  ww  w  .  j a v  a2 s.  c o m

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height) {
        return bitmap;
    }

    int len = width > height ? width : height;
    Bitmap output = Bitmap.createBitmap(len, len, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // paint.setAlpha(0);
    paint.setColor(color);
    canvas.drawPaint(paint);

    if (width > height) {
        int devide = (width - height) / 2;
        canvas.drawBitmap(bitmap, 0, devide, paint_comm);
    } else {
        int devide = (height - width) / 2;
        canvas.drawBitmap(bitmap, devide, 0, paint_comm);
    }
    return output;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    int w = 1536, h = 1280;
    Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
    Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
    android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig();
    Canvas canvas = new Canvas(bmp);

    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(frontColor);
    // text size in pixels
    paint.setTextSize((int) (400 * scale));
    // text shadow
    //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    if (backColor != -1) {
        canvas.drawColor(backColor);/*  w ww .jav  a2  s . c  om*/
    }
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bmp.getWidth() - bounds.width()) / 2;
    int y = (bmp.getHeight() + bounds.height()) / 2;
    canvas.drawText(gText, x, y, paint);
    return bmp;
}

From source file:Main.java

public static Bitmap highlightSelectedFaceThumbnail(Bitmap originalBitmap) {
    Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w  ww .ja  va 2s  .c om*/
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.parseColor("#3399FF"));
    int stokeWidth = Math.max(originalBitmap.getWidth(), originalBitmap.getHeight()) / 10;
    if (stokeWidth == 0) {
        stokeWidth = 1;
    }
    bitmap.getWidth();
    paint.setStrokeWidth(stokeWidth);
    canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap text2Bitmap(String text, int color, float size) {
    if (TextUtils.isEmpty(text)) {
        return null;
    }// w  ww  .java2 s .c  o m

    Paint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(size);
    paint.setColor(color);
    paint.setTextAlign(Paint.Align.LEFT);

    float baseline = -paint.ascent();
    int width = (int) (paint.measureText(text) + 0.5f);
    int height = (int) (baseline + paint.descent() + 0.5f);

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint);
    return image;
}

From source file:Main.java

/**
 * Frames the input bitmap in a circle.//from  w  w  w . j av  a2  s.  c  om
 */
public static Bitmap frameBitmapInCircle(Bitmap input) {
    if (input == null) {
        return null;
    }

    // Crop the image if not squared.
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int targetX, targetY, targetSize;
    if (inputWidth >= inputHeight) {
        targetX = inputWidth / 2 - inputHeight / 2;
        targetY = 0;
        targetSize = inputHeight;
    } else {
        targetX = 0;
        targetY = inputHeight / 2 - inputWidth / 2;
        targetSize = inputWidth;
    }

    // Create an output bitmap and a canvas to draw on it.
    Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Create a black paint to draw the mask.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);

    // Draw a circle.
    canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint);

    // Replace the black parts of the mask with the input image.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint);

    return output;
}

From source file:Main.java

/**
 * Changes the paint color to the specified value.
 *
 * @param paint the object to mutate with the new color
 * @param argb a 32-bit integer with eight bits for alpha, red, green, and blue,
 *        respectively/*from   w w  w  .j a  v a  2s. com*/
 */
public static void changePaint(Paint paint, int argb) {
    // TODO(user): can the following two lines can be replaced by:
    // paint.setColor(argb)?
    paint.setColor(argb & 0x00FFFFFF);
    paint.setAlpha((argb >> 24) & 0xFF);
    paint.setXfermode(null);
}