Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;

public class Main {
    /**
     * Get a circular (rounded) bitmap shape with the diameter is the smaller between target width and target height.
     * @param bitmap
     * @param width target width
     * @param height target height
     * @return Rounded (circular) bitmap width diameter is the smaller between target width and target height.
     */
    public static Bitmap getRoundedBitmap(Bitmap bitmap, int width, int height) {
        int diameter = width < height ? width : height;
        Bitmap targetBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(width / 2f, height / 2f, diameter / 2f, Path.Direction.CCW);
        canvas.clipPath(path);

        Bitmap sourceBitmap = bitmap;
        Rect destinationRect = new Rect(0, 0, width, height);
        Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
        canvas.drawBitmap(sourceBitmap, sourceRect, destinationRect, null);
        return targetBitmap;
    }
}