Java tutorial
//package com.java2s; /* * Copyright (C) Stichting Akvo (Akvo Foundation) * * This file is part of Akvo Caddisfly. * * Akvo Caddisfly is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Akvo Caddisfly is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Akvo Caddisfly. If not, see <http://www.gnu.org/licenses/>. */ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.Rect; import android.support.annotation.NonNull; public class Main { /** * Crop bitmap image into a round shape * * @param bitmap the bitmap * @param diameter the diameter of the resulting image * @return the rounded bitmap */ private static Bitmap getRoundedShape(@NonNull Bitmap bitmap, int diameter) { Bitmap resultBitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(resultBitmap); Path path = new Path(); path.addCircle(((float) diameter - 1) / 2, ((float) diameter - 1) / 2, (((float) diameter) / 2), Path.Direction.CCW); canvas.clipPath(path); resultBitmap.setHasAlpha(true); canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, diameter, diameter), null); return resultBitmap; } }