Android Bitmap Corner Round getCircleBitmap(Uri uri, Context context)

Here you can find the source of getCircleBitmap(Uri uri, Context context)

Description

get Circle Bitmap

Declaration

public static Bitmap getCircleBitmap(Uri uri, Context context) 

Method Source Code

//package com.java2s;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.net.Uri;

import android.provider.MediaStore;

import java.io.IOException;

public class Main {
    public static Bitmap getCircleBitmap(Uri uri, Context context) {
        Bitmap bm = null;//  w  w w  . j  a  v a2  s.c  om
        try {
            bm = MediaStore.Images.Media.getBitmap(
                    context.getContentResolver(), uri);
            Bitmap resized = Bitmap.createScaledBitmap(bm, 104, 104, true);
            Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
            return conv_bm;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);

            int color = 0xFF33B5E5;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 98, 98);

            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(50, 50, 50, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }
}

Related

  1. getRoundedCornerBitmap(Bitmap bitmap, float roundPx)
  2. getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels)
  3. getRoundedRectBitmap(Bitmap bitmap, int pixels)
  4. toRoundBitmap(Bitmap bitmap)
  5. toRoundBitmap(Bitmap bitmap)