Example usage for android.graphics Paint Paint

List of usage examples for android.graphics Paint Paint

Introduction

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

Prototype

public Paint() 

Source Link

Document

Create a new paint with default settings.

Usage

From source file:Main.java

public static Bitmap circleBitmap(final Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w w w . ja v a  2s .c  om
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    final float radius = width > height ? height / 2 : width / 2;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

public static Bitmap setGrayscale(Bitmap source) {
    int width, height;
    height = source.getHeight();//from www . j av  a  2  s . com
    width = source.getWidth();
    Bitmap bmpGrayScale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayScale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(source, 0, 0, paint);
    return bmpGrayScale;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    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);/*  w  ww  .  j  av a 2 s .c  o m*/
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, 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 toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from   w w  w . j  a  va  2  s .  c  o m
    width = bmpOriginal.getWidth();
    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap toOvalBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w w  w  .  ja v  a 2s  .c  o m*/
    Rect rect = new Rect(0, 0, bitmap.getHeight(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rectF, paint);
    return output;
}

From source file:Main.java

public static Bitmap toSepia(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/* w  w w. j  a v  a2  s  .co  m*/
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();

    ColorMatrix grMatrix = new ColorMatrix();
    grMatrix.setSaturation(0);

    ColorMatrix scMatrix = new ColorMatrix();
    scMatrix.setScale(1f, .85f, .72f, 1.0f);
    grMatrix.setConcat(scMatrix, grMatrix);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(grMatrix);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap roundCorners(final Bitmap source, final float radius) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  ww  w.  j  a  va 2 s .com*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;
    paint.setAntiAlias(true);//  w  ww . ja v  a2  s  .  co m
    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);
    return output;
}

From source file:Main.java

public static Bitmap setBrightness(Bitmap srcBitmap, int brightness) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0,
            brightness, 0, 0, 0, 1, 0 });
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

/**
 * Draw the face rect in the Image//from  ww w.ja v a2 s  .  co  m
 * @param canvas
 * @param face
 * @param width
 * @param height
 * @param frontCamera
 */
static public void drawFaceRect(Canvas canvas, Rect rect, int width, int height, boolean frontCamera) {

    if (canvas == null)
        return;

    Paint paint = new Paint();
    paint.setColor(Color.rgb(57, 138, 243));
    int len = (rect.bottom - rect.top) / 8;
    if (len / 8 >= 2)
        paint.setStrokeWidth(len / 8);
    else
        paint.setStrokeWidth(2);

    if (frontCamera) {
        int left = rect.left;
        rect.left = width - rect.right;
        rect.right = width - left;
    }

    paint.setStyle(Style.STROKE);
    canvas.drawRect(rect, paint);
}