Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Rect;
import android.graphics.Bitmap.Config;

public class Main {

    public static Bitmap duplicateBitmap(Bitmap bmpSrc, int width, int height) {
        if (null == bmpSrc) {
            return null;
        }

        int bmpSrcWidth = bmpSrc.getWidth();
        int bmpSrcHeight = bmpSrc.getHeight();

        Bitmap bmpDest = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        if (null != bmpDest) {
            Canvas canvas = new Canvas(bmpDest);
            Rect viewRect = new Rect();
            final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);
            if (bmpSrcWidth <= width && bmpSrcHeight <= height) {
                viewRect.set(rect);
            } else if (bmpSrcHeight > height && bmpSrcWidth <= width) {
                viewRect.set(0, 0, bmpSrcWidth, height);
            } else if (bmpSrcHeight <= height && bmpSrcWidth > width) {
                viewRect.set(0, 0, width, bmpSrcWidth);
            } else if (bmpSrcHeight > height && bmpSrcWidth > width) {
                viewRect.set(0, 0, width, height);
            }
            canvas.drawBitmap(bmpSrc, rect, viewRect, null);
        }

        return bmpDest;
    }

    public static Bitmap duplicateBitmap(Bitmap bmpSrc) {
        if (null == bmpSrc) {
            return null;
        }

        int bmpSrcWidth = bmpSrc.getWidth();
        int bmpSrcHeight = bmpSrc.getHeight();

        Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888);
        if (null != bmpDest) {
            Canvas canvas = new Canvas(bmpDest);
            final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);

            canvas.drawBitmap(bmpSrc, rect, rect, null);
        }

        return bmpDest;
    }
}