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.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    /**
     * Cut Bitmap
     * Bitmap memory size is adjusted depending on input dimensions
     * decodeCropBitmapFromResource
     */
    public static Bitmap decodeCropBitmapFromResource(Resources res, int resId, int x, int y, int width,
            int height) {

        Bitmap bitmap;
        Bitmap croppedBitmap;
        final BitmapFactory.Options options = new BitmapFactory.Options();

        // First decode with inJustDecodeBounds=true to check dimensions
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        // Enables bitmap re-usage
        options.inMutable = true;

        /* EFFICIENT Crop ? - Creates new Bitmap */// <- Best option?
        bitmap = BitmapFactory.decodeResource(res, resId, options);
        croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
        bitmap.recycle();

        /* EFFICIENT Crop ? - Reuses bitmap, but it is slow */
        //        croppedBitmap = BitmapFactory.decodeResource(res, resId, options);
        //        int[] pixels = getPixelsFromBitmap(croppedBitmap,
        //                x, y,
        //                width, height);
        //        croppedBitmap.setPixels(pixels, 0, width,
        //                0, 0,
        //                width, height);

        return croppedBitmap;
    }
}