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;
import android.graphics.RectF;

public class Main {

    public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx) {
        Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(roundCornerBitmap);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);

        int color = 0xff424242;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return roundCornerBitmap;
    }

    public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb,
            boolean rb) {
        Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx);
        Canvas canvas = new Canvas(roundCornerBitmap);
        canvas.drawARGB(0, 0, 0, 0);
        int bw = bitmap.getWidth();
        int bh = bitmap.getHeight();
        int centerW = bw / 2;
        int centerH = bh / 2;

        Paint paint = new Paint();
        Paint defaultPaint = new Paint();
        int color = 0xff424242;
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        paint.setAntiAlias(true);
        paint.setColor(color);

        Bitmap cutBitmap = null;
        Canvas cutCanvas = null;
        if (!lt) {
            cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            cutCanvas = new Canvas(cutBitmap);
            cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint);
            cutCanvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
            cutBitmap.recycle();
        }
        if (!rt) {
            cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            cutCanvas = new Canvas(cutBitmap);
            cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint);
            cutCanvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
            cutBitmap.recycle();
        }
        if (!lb) {
            cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            cutCanvas = new Canvas(cutBitmap);
            cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint);
            cutCanvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
            cutBitmap.recycle();
        }
        if (!rb) {
            cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            cutCanvas = new Canvas(cutBitmap);
            cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint);
            cutCanvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
            cutBitmap.recycle();
        }
        return roundCornerBitmap;
    }
}