Example usage for android.graphics Paint setAntiAlias

List of usage examples for android.graphics Paint setAntiAlias

Introduction

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

Prototype

public void setAntiAlias(boolean aa) 

Source Link

Document

Helper for setFlags(), setting or clearing the ANTI_ALIAS_FLAG bit AntiAliasing smooths out the edges of what is being drawn, but is has no impact on the interior of the shape.

Usage

From source file:Main.java

/**
 * ---------------------------------------------------------------------------
 * USE THIS METHOD AND NOT THE OLDER VERSION CALLED: "overlayColorOnGrayScale"
 * ---------------------------------------------------------------------------
 * Method to overlay color on a gray scale Bitmap.
 * This method creates automatically a gray scale bitmap from {@code source}.
 *
 * @param source The original colored Bitmap.
 * @param color  Color to overlay./*from  ww  w  .  jav a 2  s .c  om*/
 * @return A colored gray scale Bitmap.
 */
public static Bitmap overlayColorOnGrayScale(Bitmap source, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawBitmap(source, 0, 0, getGrayScalePaint());

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    return mutableBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx, int w, int h) {
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

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

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap clipRoundCornerBitmap(Bitmap bitmap, float radius, int borderColor) {
    if (bitmap == null) {
        return null;
    }//w  w  w  .j  av a 2 s  .co  m
    final int h = bitmap.getHeight();
    final int w = bitmap.getWidth();

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

    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(borderColor);
    canvas.drawRoundRect(rectF, radius, radius, 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 drawBackground(int cellSize, int height, int widht) {

    Bitmap bitmap = Bitmap.createBitmap(widht, height, Config.ARGB_8888);
    Canvas cv = new Canvas(bitmap);

    Paint background = new Paint();
    background.setColor(BACKGROUND_COLOR);
    cv.drawRect(0, 0, widht, height, background);
    background.setAntiAlias(true);

    background.setColor(LINE_COLOR);/*from w  ww  .j  av  a2s  .co  m*/

    for (int i = 0; i < widht / cellSize; i++) {
        cv.drawLine(cellSize * i, 0, cellSize * i, height, background);
    }

    for (int i = 0; i < height / cellSize; i++) {
        cv.drawLine(0, cellSize * i, widht, cellSize * i, background);
    }
    return bitmap;
}

From source file:Main.java

/**
 * This method takes a square bitmap and clips it into a circle
 *
 * @param bitmap : the image to clip//from   ww  w . j  a v  a  2 s . com
 * @return the clipped bitmap
 */
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
    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());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);//from  w w w.j  av  a2  s  . c  o  m
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int width, int height, float roundPx) {

    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 rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);/* w  w  w.  j a v a  2 s  .  c o  m*/
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

    return output;
}

From source file:com.dm.wallpaper.board.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res, @ColorInt int color,
        int padding) {
    try {/*from   w  w  w . j  av  a  2 s.  c  om*/
        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap srcBitmap, float radius) {
    Bitmap resultBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);//from w  ww  .j a va  2s .  c om
    Canvas canvas = new Canvas(resultBitmap);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xBDBDBE);
    canvas.drawRoundRect(rectF, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(srcBitmap, rect, rect, paint);
    return resultBitmap;
}

From source file:Main.java

/**
 * Create circle image./*w  ww .j  a  v a 2s  .  c  o  m*/
 *
 * @param bitmap      Bitmap to be cropped
 * @param resColor    Resource color
 * @param strokeWidth Thickness of stroke
 * @return Returns the circle image with border
 */
public static Bitmap getCircleImage(Bitmap bitmap, int resColor, int strokeWidth) {
    // create Bitmap to draw
    Bitmap mBitmap = Bitmap.createBitmap(bitmap.getWidth() + 8, bitmap.getHeight() + 8,
            Bitmap.Config.ARGB_8888);

    // create Rect to hold image
    final Rect mRec = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    // create Canvas
    Canvas mCanvas = new Canvas(mBitmap);
    mCanvas.drawARGB(0, 0, 0, 0);

    // create Paint
    final Paint mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);

    // get the half size of the image
    int mHalfWidth = bitmap.getWidth() / 2;
    int mHalfHeight = bitmap.getHeight() / 2;

    // draw circle
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // unknown
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // draw the image
    mCanvas.drawBitmap(bitmap, mRec, mRec, mPaint);

    // set border mode
    mPaint.setXfermode(null);

    // set stroke
    mPaint.setStyle(Paint.Style.STROKE);

    // set stroke color
    mPaint.setColor(resColor);

    // set stroke width
    mPaint.setStrokeWidth(strokeWidth);

    // draw stroke
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // return the circle image
    return mBitmap;
}