Android examples for Graphics:Bitmap Rounded
get Rounded Bitmap By Bitmap
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; public class Main { private static final String TAG = "BitmapCustomUtils"; public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int size) { Bitmap result;//from w w w. ja v a 2s .co m if (bitmap != null) { try { bitmap = getScaledBitmap(bitmap, size); // NOT CHECKING ERROR COS IF NOT RETURN NOT CHANGED BITMAP result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); int color = 0xff424242; Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(size / 2, size / 2, size / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, 0, 0, paint); return result; } catch (NullPointerException e) { e.printStackTrace(); } catch (OutOfMemoryError o) { o.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return null; } private static Bitmap getScaledBitmap(Bitmap bitmap, int size) { // TODO set up in a function if (bitmap != null && bitmap.getHeight() != size && bitmap.getWidth() != size) { // resize bitmap to accord on size float ratio; float scaledWidth = -1; float scaledHeight = -1; if ((bitmap.getHeight() < size)) { ratio = (float) size / (float) bitmap.getHeight(); scaledHeight = size; scaledWidth = (float) bitmap.getWidth() * ratio; } if ((bitmap.getWidth() < size) && scaledHeight != -1) { ratio = (float) size / (float) bitmap.getWidth(); scaledWidth = size; scaledHeight = (float) bitmap.getWidth() * ratio; } if (scaledHeight == -1 && scaledWidth == -1) { // scale on width even if they are same if (bitmap.getWidth() <= bitmap.getHeight()) { ratio = (float) size / (float) bitmap.getWidth(); scaledWidth = size; scaledHeight = (float) bitmap.getHeight() * ratio; } else if (bitmap.getWidth() > bitmap.getHeight()) { // scale on height ratio = (float) size / (float) bitmap.getHeight(); scaledHeight = size; scaledWidth = (float) bitmap.getWidth() * ratio; } } return Bitmap.createScaledBitmap(bitmap, (int) scaledWidth, (int) scaledHeight, false); } // return old bitmap return bitmap; } }