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

public class Main {
    public static Bitmap createScaledBitmap(Bitmap bitmap, int width, int height) {
        Bitmap background = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
        float originalWidth = bitmap.getWidth(), originalHeight = bitmap.getHeight();
        Canvas canvas = new Canvas(background);
        float scale = Math.max(width / originalWidth, height / originalHeight);
        float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale) / 2.0f;
        if (originalWidth < originalHeight) {
            // scale = height / originalHeight;
            xTranslation = (width - originalWidth * scale) / 2.0f;
            yTranslation = 0.0f;
        }
        Matrix transformation = new Matrix();
        transformation.postTranslate(xTranslation, yTranslation);
        transformation.preScale(scale, scale);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        canvas.drawBitmap(bitmap, transformation, paint);
        return background;
    }
}