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;

import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;

public class Main {
    public static Bitmap getSizedBitmap(@NonNull Resources res, @DrawableRes int resId, int desiredWidth) {
        // Load just the size of the image
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Options now has the bounds; prepare it for getting the actual image
        options.inSampleSize = 1;
        options.inJustDecodeBounds = false;

        if (options.outWidth > desiredWidth) {
            final int halfWidth = options.outWidth / 2;

            while (halfWidth / options.inSampleSize > desiredWidth) {
                options.inSampleSize *= 2;
            }
        }

        return BitmapFactory.decodeResource(res, resId, options);
    }
}