Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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 {
    public static Bitmap formatUserAvatar(Bitmap photo) {
        int maskColor = 0xff424242;
        Paint cornerPaint = new Paint();
        cornerPaint.setAntiAlias(true);
        cornerPaint.setColor(maskColor);
        Rect roundedCornerRect = new Rect(0, 0, 256, 256);
        RectF roundedCornerRectF = new RectF(roundedCornerRect);
        Bitmap roundedCornerBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
        Canvas roundedCornerCanvas = new Canvas(roundedCornerBitmap);
        roundedCornerCanvas.drawARGB(0, 0, 0, 0);
        roundedCornerCanvas.drawRoundRect(roundedCornerRectF, 128, 128, cornerPaint);
        cornerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(photo, 256, 256, true);
        roundedCornerCanvas.drawBitmap(scaledBitmap, roundedCornerRect, roundedCornerRect, cornerPaint);
        return roundedCornerBitmap;
    }
}