Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Matrix;
import android.graphics.Paint;

import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import android.util.TypedValue;

public class Main {
    public static final int SCALE_TYPE_FIT_XY = 0;
    public static final int SCALE_TYPE_FIT_START = 1;

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips,
            Context context, boolean recycleOrig) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
                context.getResources().getDisplayMetrics());
        final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
                context.getResources().getDisplayMetrics());
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        // prepare canvas for transfer
        paint.setAntiAlias(true);
        paint.setColor(0xFFFFFFFF);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

        // draw bitmap
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        // draw border
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) borderSizePx);
        canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

        if (recycleOrig && bitmap != null && !bitmap.isRecycled())
            bitmap.recycle();

        return output;
    }

    /**
     * create scaled bitmap with required width and height
     *
     * @param srcBitmap
     * @param reqWidth
     * @param reqHeight
     * @param recycleOrig
     * @param scaleType
     * @return
     */
    public synchronized static Bitmap createBitmap(Bitmap srcBitmap, int reqWidth, int reqHeight,
            boolean recycleOrig, int scaleType) {
        int bitmapWidth = srcBitmap.getWidth();
        int bitmapHeight = srcBitmap.getHeight();
        if (reqWidth == 0)
            reqWidth = bitmapWidth;
        if (reqHeight == 0)
            reqHeight = bitmapHeight;

        //        final Rect srcRect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
        //        final Rect dstRect = new Rect(0, 0, reqWidth, reqHeight);
        float scaleWidth = 1;
        float scaleHeight = 1;
        if (scaleType == SCALE_TYPE_FIT_START) {
            scaleWidth = (reqWidth / bitmapWidth < reqHeight / bitmapHeight)
                    ? (float) reqWidth / (float) bitmapWidth
                    : (float) reqHeight / (float) bitmapHeight;
            scaleHeight = scaleWidth;
        } else if (scaleType == SCALE_TYPE_FIT_XY) {
            scaleWidth = (float) reqWidth / (float) bitmapWidth;
            scaleHeight = (float) reqHeight / (float) bitmapHeight;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

        if (recycleOrig) {
            srcBitmap.recycle();
        }

        return resizedBitmap;
    }
}