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.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

public class Main {
    /**
     * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap,
     * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside.
     *
     * @return
     */
    public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) {
        Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
        clearBitmap.eraseColor(color);

        Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor);

        int frameWidth = clearBitmap.getWidth();
        int frameHeight = clearBitmap.getHeight();
        int imageWidth = resizedInsideBitmap.getWidth();
        int imageHeight = resizedInsideBitmap.getHeight();

        Canvas canvas = new Canvas(clearBitmap);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2,
                paint);
        return clearBitmap;
    }

    public static Bitmap scaleBitmapByFactor(Bitmap source, float factor) {
        int newWidth = (int) (source.getWidth() * factor);
        int newHeight = (int) (source.getHeight() * factor);
        return Bitmap.createScaledBitmap(source, newWidth, newHeight, true);
    }
}