Example usage for android.content.res Resources getValueForDensity

List of usage examples for android.content.res Resources getValueForDensity

Introduction

In this page you can find the example usage for android.content.res Resources getValueForDensity.

Prototype

public void getValueForDensity(@AnyRes int id, int density, TypedValue outValue, boolean resolveRefs)
        throws NotFoundException 

Source Link

Document

Get the raw value associated with a resource with associated density.

Usage

From source file:Main.java

public static Bitmap getBitmapForDensity(Resources res, int displayDpi, int resId) {
    try {//from ww  w.j  a  va  2  s  .c  om
        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);
    }
}