Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 CanvasProfile.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Main {
    /**
     * draw the image to viewBitmap in scale mode.
     * @param viewBitmap Bitmap to be displayed on the device.
     * @param bitmap Bitmap image.
     */
    public static void drawImageForScalesMode(final Bitmap viewBitmap, final Bitmap bitmap) {

        float startGridX = 0;
        float startGridY = 0;

        float getSizeW = bitmap.getWidth();
        float getSizeH = bitmap.getHeight();

        float scale;
        final int width = viewBitmap.getWidth();
        final int height = viewBitmap.getHeight();
        if ((getSizeW / width) > (getSizeH / height)) {
            scale = width / getSizeW;
        } else {
            scale = height / getSizeH;
        }

        int targetW = (int) Math.ceil(scale * getSizeW);
        int targetH = (int) Math.ceil(scale * getSizeH);

        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, targetW, targetH, false);

        if ((getSizeW / width) > (getSizeH / height)) {
            startGridY = (height / 2 - targetH / 2);
        } else {
            startGridX = (width / 2 - targetW / 2);
        }

        Canvas canvas = new Canvas(viewBitmap);
        canvas.drawBitmap(resizedBitmap, startGridX, startGridY, null);
    }
}