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(Paint paint) 

Source Link

Document

Create a new paint, initialized with the attributes in the specified paint parameter.

Usage

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  w w .  j  a va 2s . c  o  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

/**
 * Use touch-icon or higher-resolution favicon and round the corners.
 * @param context    Context used to get resources.
 * @param touchIcon  Touch icon bitmap.//from   www  .  j  a  v a2  s .  c om
 * @param canvas     Canvas that holds the touch icon.
 */
private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);
    // Convert dp to px.
    int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOUCHICON_BORDER_RADII,
            context.getResources().getDisplayMetrics());
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
    path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

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  a  va2  s .c om*/
    // 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  ww  .jav  a  2s.c o 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

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    if (bitmap == null) {
        return null;
    }//from   w w w.  ja va2s. co  m
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}

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);/*from   w  w  w .j a  v  a2  s.  co 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 w w  .  j  a va 2 s  . co  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 {//ww w.j av  a2s  .  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 cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)/*from  www .j  ava2 s  .c o  m*/
        bitmap.recycle();
    return target;
}

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./*from   ww  w.  j av a2s. 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;
}