Example usage for android.content.res Resources getDrawableForDensity

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

Introduction

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

Prototype

@Nullable
public Drawable getDrawableForDensity(@DrawableRes int id, int density, @Nullable Theme theme) 

Source Link

Document

Return a drawable object associated with a particular resource ID for the given screen density in DPI and styled for the specified theme.

Usage

From source file:Main.java

/**
 * @see android.content.res.Resources#getDrawableForDensity(int id, int density).
 *///from w w  w.  j  a  va  2s. com
@SuppressWarnings("deprecation")
public static Drawable getDrawableForDensity(Resources res, int id, int density) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawableForDensity(id, density, null);
    } else {
        return res.getDrawableForDensity(id, density);
    }
}

From source file:jahirfiquitiva.iconshowcase.tasks.LoadAppsToRequest.java

@SuppressWarnings("deprecation")
public Drawable getAppIcon(Resources resources, int iconId) {
    Drawable d;/*from   w  w  w .ja  v a  2  s .com*/
    try {
        int iconDpi;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            iconDpi = DisplayMetrics.DENSITY_XXXHIGH;
        } else {
            iconDpi = DisplayMetrics.DENSITY_XXHIGH;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            d = resources.getDrawableForDensity(iconId, iconDpi, null);
        } else {
            d = resources.getDrawableForDensity(iconId, iconDpi);
        }

    } catch (Resources.NotFoundException e) {
        try {
            d = ContextCompat.getDrawable(context.get(), R.drawable.ic_na_launcher);
        } catch (Resources.NotFoundException e1) {
            d = null;
        }
    }

    return (d != null) ? d : getAppDefaultActivityIcon();
}

From source file:de.baumann.thema.RequestActivity.java

private Drawable getHighResIcon(PackageManager pm, ResolveInfo resolveInfo) {

    Resources resources;
    Drawable icon;//from  www.ja  v  a 2 s.c o m

    try {
        ComponentName componentName = new ComponentName(resolveInfo.activityInfo.packageName,
                resolveInfo.activityInfo.name);

        resources = pm.getResourcesForActivity(componentName);//Get resources for the activity

        int iconId = resolveInfo.getIconResource();//Get the resource Id for the activity icon

        if (iconId != 0) {
            icon = resources.getDrawableForDensity(iconId, 640, null); //Loads the icon at xxhdpi resolution or lower.
            return icon;
        }
        return resolveInfo.loadIcon(pm);

    } catch (PackageManager.NameNotFoundException e) {
        return resolveInfo.loadIcon(pm);//If it fails return the normal icon
    } catch (Resources.NotFoundException e) {
        return resolveInfo.loadIcon(pm);
    }
}