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

/**
 * Adds tint effect to target bitmap./*  w  w  w  .j a  v a2 s  . c  o m*/
 *
 * @param src
 *            PNG bitmap to apply tint affect
 * @param argb
 *            tint color
 * @return bitmap with tint affect
 */
public static Bitmap addTintEffect(Bitmap src, int argb) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(argb, PorterDuff.Mode.SRC_OVER));

    Canvas bottomCanvas = new Canvas(src);
    bottomCanvas.drawBitmap(src, 0, 0, paint);

    return src;
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap, int round) {
    if (bitmap == null)
        return null;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        if (round != 0)
            roundPx = round;/*  w ww.  ja  v a 2 s .c om*/
        else
            roundPx = width / 2;
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        if (round != 0)
            roundPx = round;
        else
            roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }

    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 src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

/**
 * Gets the rounded corner bitmap.//from   w  ww.  ja  v  a  2  s . c o m
 * 
 * @param bitmap
 *            the bitmap
 * @return the rounded corner bitmap
 */
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, Boolean create_circle) {
    DisplayMetrics mMetrics = context.getResources().getDisplayMetrics();
    float mScaleFactor = mMetrics.density;
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = Color.BLACK;
    final Paint paint = new Paint();
    int width = bitmap.getWidth();
    int height = (bitmap.getHeight() > width) ? width : bitmap.getHeight();
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);
    final float roundPx = (create_circle) ? (bitmap.getWidth() > 360) ? bitmap.getWidth() : 360 : 2;

    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    // draw border
    paint.setColor(Color.parseColor("#cccccc"));
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(0.5F * mScaleFactor);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    return output;
}

From source file:Main.java

/**
 * Returns a Bitmap representing the thumbnail of the specified Bitmap. The
 * size of the thumbnail is defined by the dimension
 * android.R.dimen.launcher_application_icon_size.
 * <p>//from www . ja va 2  s.co  m
 * This method is not thread-safe and should be invoked on the UI thread
 * only.
 *
 * @param bitmap  The bitmap to get a thumbnail of.
 * @param context The application's context.
 * @return A thumbnail for the specified bitmap or the bitmap itself if the
 * thumbnail could not be created.
 */
public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) {
    int sIconWidth = -1;
    int sIconHeight = -1;
    final Resources resources = context.getResources();
    sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);

    final Paint sPaint = new Paint();
    final Rect sBounds = new Rect();
    final Rect sOldBounds = new Rect();
    Canvas sCanvas = new Canvas();

    int width = sIconWidth;
    int height = sIconHeight;

    sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    if (width > 0 && height > 0) {
        if (width < bitmapWidth || height < bitmapHeight) {
            final float ratio = (float) bitmapWidth / bitmapHeight;

            if (bitmapWidth > bitmapHeight) {
                height = (int) (width / ratio);
            } else if (bitmapHeight > bitmapWidth) {
                width = (int) (height * ratio);
            }

            final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig()
                    : Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
            sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
            canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
            return thumb;
        } else if (bitmapWidth < width || bitmapHeight < height) {
            final Config c = Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint);
            return thumb;
        }
    }

    return bitmap;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *//*www . jav  a  2 s. c  o m*/
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawOval(dst, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *///from   ww  w  .  j  a va2s  . com
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    canvas.drawOval(0, 0, targetWidth, targetHeight, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

/**
 * This method is used to create bitmap with rounded corners.
 * //from www  . j  a  v a2 s .c o m
 * @param bitmap
 *            which is to be rouded.
 * @param pixels
 *            dimension of the resultant image
 * @return new bitmap with the specified dimension
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    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 = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    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 createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) {
    if (null == srcBitmap) {
        return null;
    }//from  ww w . j  a v  a 2  s  . c o  m

    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    int reflectionWidth = srcBitmap.getWidth();
    int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight);

    if (0 == srcWidth || srcHeight == 0) {
        return null;
    }

    // The matrix
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    try {
        // The reflection bitmap, width is same with original's
        Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight,
                reflectionWidth, reflectionHeight, matrix, false);

        if (null == reflectionBitmap) {
            return null;
        }

        Canvas canvas = new Canvas(reflectionBitmap);

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

        LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmap.getHeight(), 0x70FFFFFF,
                0x00FFFFFF, TileMode.MIRROR);
        paint.setShader(shader);

        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));

        // Draw the linear shader.
        canvas.drawRect(0, 0, reflectionBitmap.getWidth(), reflectionBitmap.getHeight(), paint);

        return reflectionBitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Bitmap roundCornerFromFile(String filePath, int pixels, int size) {
    try {//from w  ww.  j  av a2 s  .  c om
        Bitmap bitmap = loadFile(filePath, size);
        if (bitmap == null)
            return null;
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        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);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        bitmap.recycle();
        bitmap = null;
        return output;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;// ww w .  ja v  a  2 s. c  om
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);
    paint.setAntiAlias(true);
    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, src, dst, paint);
    bitmap.recycle();
    return output;
}