Wednesday, March 23, 2016

How to make a rounded bitmap in Android

    public static Bitmap getRoundedBitmap(Bitmap bitmap, int i, int i2) {
        if (bitmap == null) {
            return null;
        }
        Bitmap createBitmap = Bitmap.createBitmap(i, i2, bitmap.getConfig());
        Canvas canvas = new Canvas(createBitmap);
        Paint paint = new Paint();
        canvas.drawARGB(0, 0, 0, 0);
        paint.setAntiAlias(true);
        canvas.drawOval(0.0f, 0.0f, (float) i, (float) i2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float min = Math.min(((float) width) / ((float) i), ((float) height) / ((float) i2));
        int i3 = (int) ((((float) i) * min) / 2.0f);
        int i4 = (int) ((min * ((float) i2)) / 2.0f);
        canvas.drawBitmap(bitmap, new Rect((width / 2) - i3, (height / 2) - i4, (width / 2) + i3, (height / 2) + i4), new RectF(0.0f, 0.0f, (float) i, (float) i2), paint);
        return createBitmap;
    }