Example usage for android.graphics Canvas drawARGB

List of usage examples for android.graphics Canvas drawARGB

Introduction

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

Prototype

public void drawARGB(int a, int r, int g, int b) 

Source Link

Document

Fill the entire canvas' bitmap (restricted to the current clip) with the specified ARGB color, using srcover porterduff mode.

Usage

From source file:com.silentcircle.common.util.ViewUtil.java

/**
 * Cut a circular image from provided bitmap.
 *
 * @param bitmap Bitmap from which to cut the circle.
 *
 *///from ww  w.  j av a 2s.c om
public static Bitmap getCircularBitmap(final Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int dimens = width;
    if (width > height) {
        dimens = height;
    }
    Bitmap output = Bitmap.createBitmap(dimens, dimens, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final float radius = width / 2.0f;
    final int color = 0xffff00ff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawCircle(radius, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

private static synchronized Bitmap createScaledBitmap(Bitmap bitmap, final int width, final int height,
        float cornerRadius) {

    if (bitmap == null) {
        return null;
    }/*from  ww  w .j a v a 2  s .c om*/

    int adjustedWidth = width;
    int adjustedHeight = height;

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

    //int inBytes = bitmap.getByteCount();
    if (width >= bitmapWidth && height >= bitmapHeight)
        return bitmap;

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

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

        final Bitmap.Config c = Bitmap.Config.ARGB_8888;
        final Bitmap thumb = Bitmap.createBitmap(width, height, c);
        final Canvas canvas = sScaleCanvas;
        final Paint paint = sPaint;
        canvas.setBitmap(thumb);
        paint.setDither(false);
        paint.setFilterBitmap(true);

        Rect sBounds = new Rect();
        Rect sOldBounds = new Rect();

        sBounds.set((width - adjustedWidth) >> 1, (height - adjustedHeight) >> 1, adjustedWidth,
                adjustedHeight);
        sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);

        if (cornerRadius != 0) {
            //Path p = new Path();
            RectF rect = new RectF(sBounds);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.WHITE);
            canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            //p.addRoundRect(rect, cornerRadius, cornerRadius, Direction.CCW);
            //canvas.clipPath(p, Op.REPLACE);
        } else {
            paint.setXfermode(null);
            //canvas.clipRect(0, 0, thumb.getWidth(), thumb.getHeight());
        }

        canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);

        canvas.setBitmap(Bitmap.createBitmap(1, 1, Config.ALPHA_8));

        return thumb;

    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    //final Paint paintBorder = new Paint();
    // paintBorder.setColor(Color.GREEN);
    //paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
    //BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(output, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    //paint.setShader(shader);
    paint.setAntiAlias(true);//from  w  ww . j a v a  2  s.  c o  m
    //paintBorder.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);
    //int circleCenter = bitmap.getWidth() / 2;
    //int borderWidth = 2;
    //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
    //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //canvas.drawBitmap(bitmap, circleCenter + borderWidth, circleCenter + borderWidth, paint);
    bitmap.recycle();
    return output;
}

From source file:net.archenemy.archenemyapp.presenter.BitmapUtility.java

/**
 * Transforms a bitmap into a circle shape.
 * /*from w w w .  ja  v  a2  s.c o  m*/
 * @param bitmap
 * @param pixels
 * @return bitmap as circle shape
 */
public static Bitmap getCircleBitmap(Bitmap bitmap, int diameterPixels) {
    if (bitmap != null) {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        final 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 = diameterPixels;

        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;
    }
    return null;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap getRoundedCornerBitmap(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());

    final RectF rectF = new RectF(rect);

    final float roundPx = 12;

    paint.setAntiAlias(true);//  ww  w  .j a  v a  2 s . c  o 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 getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips,
        Context context, boolean recycleOrig) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
            context.getResources().getDisplayMetrics());
    final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
            context.getResources().getDisplayMetrics());
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    // prepare canvas for transfer
    paint.setAntiAlias(true);//  w w w.  j a  v a  2  s .  c  o  m
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

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

    // draw border
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) borderSizePx);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    if (recycleOrig && bitmap != null && !bitmap.isRecycled())
        bitmap.recycle();

    return output;
}

From source file:ua.com.spacetv.mycookbook.fragments.FragTextRecipe.java

/**
 * Rounding corners on bitmap/* ww w . j  a  v a2  s . c o  m*/
 *
 * @param bitmap image of recipe
 * @param pixels - radius
 * @return processed bitmap
 */
public static Bitmap getRoundedCornerBitmap(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);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, pixels, pixels, paint);

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

    return output;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./*from  w w  w  . j a v a 2 s .  c o m*/
 *
 * @param rawBitmap bitmap to round corners
 */
public static Bitmap publishAvatar(Bitmap rawBitmap, int roundK) {
    if (rawBitmap == null)
        return null;

    try {
        int size = 0;
        if (rawBitmap.getHeight() > rawBitmap.getWidth()) {
            size = rawBitmap.getWidth();
        } else {
            size = rawBitmap.getHeight();
        }
        Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
        final RectF rectF = new RectF(rect);
        final float roundPx = roundK;

        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(rawBitmap, rect, rect, paint);

        rawBitmap.recycle();

        return output;
    } catch (Exception e) {

    }

    return null;
}

From source file:nu.yona.app.utils.AppUtils.java

/**
 * Gets circle bitmap.//  ww  w .j  a  v  a  2 s .com
 *
 * @param bitmap the bitmap
 * @return the circle bitmap
 */
public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.BLUE;
    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);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar.//from ww  w  .j a  v  a2  s  .c o  m
 *
 * @param filePath file path to avatar image
 */
public static Bitmap publishAvatar(String filePath) {
    Bitmap bitmap = null;

    if (!TextUtils.isEmpty(filePath)) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filePath, opts);
            int size = 0;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                size = bitmap.getWidth();
            } else {
                size = bitmap.getHeight();
            }
            Bitmap output = Bitmap.createBitmap(size, size, 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, size, size);
            final RectF rectF = new RectF(rect);
            final float roundPx = 12;

            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, rect, rect, paint);

            bitmap.recycle();

            return output;
        } catch (Exception e) {

        }
    }
    return null;
}