Example usage for android.graphics Paint ANTI_ALIAS_FLAG

List of usage examples for android.graphics Paint ANTI_ALIAS_FLAG

Introduction

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

Prototype

int ANTI_ALIAS_FLAG

To view the source code for android.graphics Paint ANTI_ALIAS_FLAG.

Click Source Link

Document

Paint flag that enables antialiasing when drawing.

Usage

From source file:Main.java

public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }//from   ww  w.  j  av  a2s  .  c o m
    // 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 - #808080
    paint.setColor(Color.rgb(127, 127, 127));
    // text size in pixels
    paint.setTextSize((int) (14 * scale * 5));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    //      int x = (bitmap.getWidth() - bounds.width()) / 2;
    //      int y = (bitmap.getHeight() + bounds.height()) / 2;
    //draw  text  to the bottom
    int x = (bitmap.getWidth() - bounds.width()) / 10 * 9;
    int y = (bitmap.getHeight() + bounds.height()) / 10 * 9;
    canvas.drawText(gText, x, y, paint);

    return bitmap;
}

From source file:Main.java

/**
 * mask bitmap//from w w w.j  a va  2 s  . co m
 * @param src todo
 * @param mask todo
 * @param dst todo
 * @param dstCanvas todo
 * @param paint todo
 * @param paintMode todo
 * @return todo
 */
public static Bitmap maskBitmap(Bitmap src, Bitmap mask, Bitmap dst, Canvas dstCanvas, Paint paint,
        PorterDuffXfermode paintMode) {
    if (dst == null)
        dst = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);

    if (dstCanvas == null)
        dstCanvas = new Canvas(dst);

    if (paintMode == null)
        paintMode = new PorterDuffXfermode(Mode.DST_IN);

    if (paint == null) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(paintMode);
    }

    dstCanvas.drawBitmap(src, 0, 0, null);
    dstCanvas.drawBitmap(mask, 0, 0, paint);

    paint.setXfermode(null);

    return dst;
}

From source file:Main.java

private static Paint getTextPaint(float size) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShadowLayer(1, 1, 1, 0xFF000000);
    paint.setColor(0xFFFFFFFF);/*w  w  w.ja v a  2  s .  c o  m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(size);
    paint.setTypeface(Typeface.DEFAULT);
    return paint;

}

From source file:Main.java

private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) {
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());

    // Paint used for scaling the bitmap and drawing the rounded rect.
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);//  w  ww  .j  a v a 2 s  .c  o  m
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);

    // Construct a path from a round rect. This will allow drawing with
    // an inverse fill so we can punch a hole using the round rect.
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(1, 1);
    path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);

    // Reuse the paint and clear the outside of the rectangle.
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {//from   w w  w  .  ja v a 2s  . co  m
        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 drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int downSampling, String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);//from  w  w  w .  ja v  a2s .  c om
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap.// w w  w.  j a va  2s.c o  m
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) {
    // Canvas canvas = new Canvas(bitmap);
    // Rect r =new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    // RectF rect = new RectF(r);
    // Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
    // canvas.drawRoundRect(rect, 3f, 3f, paint);
    // // canvas.drawBitmap(bitmap, 0, 0, paint);
    // return bitmap;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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 = radius;
    paint.setAntiAlias(true);/*from w w  w.  j a  va  2  s.  c  o  m*/
    // canvas.drawARGB(0, 0, 0, 0);
    // paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, new Paint(Paint.ANTI_ALIAS_FLAG));
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

/**
 * Get a mask of color {@code color} using the alpha channel of {@code source}.
 * @param source Source image containing the shape alpha channel.
 * @param color Color of the shape.//  w w  w .  j a va  2 s  .  co  m
 * @return A {@link Bitmap} containing the shape of the given color.
 */
private static Bitmap getMask(Bitmap source, int color) {
    Bitmap retVal;

    retVal = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    retVal.eraseColor(color);

    synchronized (canvas) {
        if (maskPaint == null) {
            maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        }

        canvas.setBitmap(retVal);
        canvas.drawBitmap(source, 0, 0, maskPaint);
    }

    return retVal;
}

From source file:Main.java

private static Paint getGlossPaint() {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0x22FFFFFF);//w w  w .j  a v  a 2  s .co m
    return paint;
}