Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;

public class Main {
    /**Method to get round shaped bitmap. Mostly used for profile pictures
     *
     *     @param scaleBitmapImage
     *                      The source bitmap which has to converted to round shape
     *     @param context
     *                      The context
     *     @param targetWidthPixels
     *                      The width required for the target or returned  bitmap
     *     @param targetHeightPixels
     *                      The height required for the target or returned  bitmap
     *     @return
     *            round shaped bitmap
     */
    public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, Context context, int targetWidthPixels,
            int targetHeightPixels) {

        Bitmap targetBitmap = Bitmap.createBitmap(targetWidthPixels, targetHeightPixels, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidthPixels - 1) / 2, ((float) targetHeightPixels - 1) / 2,
                (Math.min(((float) targetWidthPixels), ((float) targetHeightPixels)) / 2), Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
                new Rect(0, 0, targetWidthPixels, targetHeightPixels), null);
        return targetBitmap;
    }
}