Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap readBitmapFormSrc(Context context, int src, float width, float height, int pixels) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(context.getResources(), src, options);
        options.inSampleSize = calculateInSampleSize(options, width, height, pixels);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), src, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, float width, float height, int pixels) {
        width = calculateSizeAsPixels(width, pixels);
        height = calculateSizeAsPixels(height, pixels);

        int originalWidth = options.outWidth;
        int originalHeight = options.outHeight;
        int inSampleSize = 1;

        if (originalWidth > width || originalHeight > height) {
            int halfWidth = originalWidth / 2;
            int halfHeight = originalHeight / 2;
            while ((halfWidth / inSampleSize > width) && (halfHeight / inSampleSize > height)) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    private static int calculateSizeAsPixels(float size, int pixels) {
        return (int) (size * (pixels / 1080.0));
    }
}