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.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

public class Main {

    public static Bitmap createCircleBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }

        int bmWidth = bitmap.getWidth();
        int bmHeight = bitmap.getHeight();
        int side = bmWidth < bmHeight ? bmWidth : bmHeight;
        int x = (bmWidth - side) / 2;
        int y = (bmHeight - side) / 2;
        Bitmap newBm = Bitmap.createBitmap(side, side, Bitmap.Config.ARGB_8888);
        if (newBm != null) {
            Canvas canvas = new Canvas(newBm);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            paint.setDither(true);

            Rect rect = new Rect(0, 0, newBm.getWidth(), newBm.getHeight());
            canvas.drawARGB(0, 0, 0, 0);
            canvas.drawCircle(newBm.getWidth() / 2, newBm.getHeight() / 2, newBm.getHeight() / 2, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

            return newBm;
        }
        return null;
    }
}