Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

public class Main {
    public static Bitmap getRoundImage(Bitmap oriImg, boolean recycleOld) {

        Bitmap targetBitmap = null;

        if (oriImg != null && !oriImg.isRecycled()) {
            int size = Math.min(oriImg.getWidth(), oriImg.getHeight());
            int targetWidth = size;
            int targetHeight = size;

            targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(targetBitmap);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            canvas.drawARGB(0, 0, 0, 0);
            canvas.drawCircle(targetWidth / 2f, targetHeight / 2f, targetWidth / 2f, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(oriImg, new Rect(0, 0, size, size), new Rect(0, 0, targetWidth, targetHeight), paint);

            if (recycleOld) {
                oriImg.recycle();
            }
        }
        return targetBitmap;
    }
}