Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.res.AssetFileDescriptor;

import android.content.res.Resources;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import android.util.TypedValue;

public class Main {
    public static Bitmap getBitmapForDensity(Resources res, int displayDpi, int resId) {
        try {
            TypedValue value = new TypedValue();
            res.getValueForDensity(resId, displayDpi, value, true);
            AssetFileDescriptor fd = res.getAssets().openNonAssetFd(value.assetCookie, value.string.toString());
            Options opt = new Options();
            opt.inTargetDensity = displayDpi;
            Bitmap bitmap = BitmapFactory.decodeResourceStream(res, value, fd.createInputStream(), null, opt);
            bitmap.setDensity(res.getDisplayMetrics().densityDpi);
            fd.close();
            return bitmap;
        } catch (Exception e) {
            return BitmapFactory.decodeResource(res, resId);
        }
    }
}