Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

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

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Applies a circular mask to a bitmap.//  w  w  w .j a  v a 2 s.  c  o m
 *
 * @param bitmap The bitmap to apply the mask to.
 */
public static Bitmap applyCircularMask(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setAntiAlias(true);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:com.chalmers.feedlr.adapter.FeedAdapter.java

/**
 * //from   w w w.j av a 2s  .c o  m
 * @param squareBitmap
 *            original image
 * @return image with rounded corners
 */
public static Bitmap getRoundedCornerBitmap(Bitmap squareBitmap) {
    Bitmap roundedBitmap = Bitmap.createBitmap(squareBitmap.getWidth(), squareBitmap.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, squareBitmap.getWidth(), squareBitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 8;

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

    return roundedBitmap;
}

From source file:Main.java

public static Bitmap getRoundedRectBitmap(Bitmap bitmap) {
    Bitmap result = null;/*from  w  w w .j  av  a 2  s.  c o m*/
    Canvas canvas;
    Paint paint;
    try {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        int color = 0xff424242;
        float radius = width > height ? width / 2 : height / 2;
        paint = new Paint();
        Rect rect = new Rect(0, 0, width, height);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (NullPointerException | OutOfMemoryError e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Gets the rounded corner bitmap.//w ww. j a va2 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:com.bobomee.android.common.util.ScreenUtil.java

/**
 * https://gist.github.com/PrashamTrivedi/809d2541776c8c141d9a
 *///from w w  w .j av a  2  s  . co m
public static Bitmap shotRecyclerView(RecyclerView view) {
    RecyclerView.Adapter adapter = view.getAdapter();
    Bitmap bigBitmap = null;
    if (adapter != null) {
        int size = adapter.getItemCount();
        int height = 0;
        Paint paint = new Paint();
        int iHeight = 0;
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;
        LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
        for (int i = 0; i < size; i++) {
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
            adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
                    holder.itemView.getMeasuredHeight());
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            Bitmap drawingCache = holder.itemView.getDrawingCache();
            if (drawingCache != null) {

                bitmaCache.put(String.valueOf(i), drawingCache);
            }
            height += holder.itemView.getMeasuredHeight();
        }

        bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);
        Drawable lBackground = view.getBackground();
        if (lBackground instanceof ColorDrawable) {
            ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
            int lColor = lColorDrawable.getColor();
            bigCanvas.drawColor(lColor);
        }

        for (int i = 0; i < size; i++) {
            Bitmap bitmap = bitmaCache.get(String.valueOf(i));
            bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
            iHeight += bitmap.getHeight();
            bitmap.recycle();
        }
    }
    return bigBitmap;
}

From source file:com.nloko.android.Utils.java

public static Bitmap crop(Bitmap bitmapToCrop, int destHeight, int destWidth) {
    int width = bitmapToCrop.getWidth();
    int height = bitmapToCrop.getHeight();
    if (width <= destWidth && height <= destHeight) {
        return bitmapToCrop;
    }//from ww w.ja v a  2s. com

    Bitmap b = Bitmap.createBitmap(destHeight, destWidth, Bitmap.Config.RGB_565);
    Canvas c1 = new Canvas(b);

    int midpointX = width / 2;
    int midpointY = height / 2;

    Rect r = new Rect(midpointX - destWidth / 2, midpointY - destHeight / 2, midpointX + destWidth / 2,
            midpointY + destHeight / 2);

    int left = 0; //(width / 2) - (bitmapToCrop.getWidth() / 2);
    int top = 0; //(height / 2) - (bitmapToCrop.getWidth() / 2);
    c1.drawBitmap(bitmapToCrop, r, new Rect(left, top, left + destWidth, top + destHeight), null);

    return b;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float pixels) {
    if (bitmap == null) {
        Log.e(TAG, "getRoundedCornerBitmap - null bitmap");
        return null;
    }//from   w  w  w  . java 2 s.c o  m

    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:org.jared.synodroid.ds.utils.Utils.java

/**
 * Create a rounded bitmap//w  w  w. j a v a 2  s  .  c  o m
 * 
 * @param bitmap
 *            The original bitmap
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    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);

    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

/**
 * Transform source Bitmap to targeted width and height.
 *///  ww w. ja  v a2  s .co m
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) {
    boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0;
    boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0;

    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
        /*
         * In this case the bitmap is smaller, at least in one dimension,
         * than the target.  Transform it by placing as much of the image
         * as possible into the target and leaving the top/bottom or
         * left/right (or both) black.
         */
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b2);

        int deltaXHalf = Math.max(0, deltaX / 2);
        int deltaYHalf = Math.max(0, deltaY / 2);
        Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()),
                deltaYHalf + Math.min(targetHeight, source.getHeight()));
        int dstX = (targetWidth - src.width()) / 2;
        int dstY = (targetHeight - src.height()) / 2;
        Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
        c.drawBitmap(source, src, dst, null);
        if (recycle) {
            source.recycle();
        }
        return b2;
    }
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
    } else {
        b1 = source;
    }

    if (recycle && b1 != source) {
        source.recycle();
    }

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);

    if (b2 != b1) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:com.amazon.android.utils.Helpers.java

/**
 * Rounds the corners of an image.//  w  ww .  jav  a 2s  .  c  o m
 *
 * @param activity The activity.
 * @param raw      The raw bitmap image to round.
 * @param round    The radius for the round corners.
 * @return The rounded image.
 */
public static Bitmap roundCornerImage(Activity activity, Bitmap raw, float round) {

    int width = raw.getWidth();
    int height = raw.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(ContextCompat.getColor(activity, android.R.color.black));

    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    canvas.drawRoundRect(rectF, round, round, paint);

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

    return result;
}