Java tutorial
//package com.java2s; // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 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.graphics.RectF; public class Main { private static Context sApplicationContext; /** * Creates a bitmap with rounded corners * * @param bitmap source bitmap * @param maxHeight maximal height for result bitmap * @param pixels corner radius * @return a new bitmap if succeed, nil instead */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) { if (bitmap == null || sApplicationContext == null) { return bitmap; } maxHeight = (int) (sApplicationContext.getResources().getDisplayMetrics().density * maxHeight); float scale = bitmap.getHeight() * 1.0f / maxHeight; int newWidth = (int) (bitmap.getWidth() / scale); Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffffffff; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final Rect dstRect = new Rect(0, 0, newWidth, maxHeight); final RectF rectF = new RectF(dstRect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, dstRect, paint); return output; } }