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
@Deprecated
public Drawable getDrawableForDensity(@DrawableRes int id, int density) throws NotFoundException 

Source Link

Document

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

Usage

From source file:com.silentcircle.contacts.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *///from  w w w. j a  v a  2 s . c  om
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(ContextCompat.getColor(mContext, R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f,
                ContextCompat.getColor(mContext, R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.android.contacts.common.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *///  w  ww .j  ava  2 s. c o m
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*from  w  w w . j a v a2  s .co  m*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    final Drawable phoneDrawable = r.getDrawableForDensity(actionResId, mIconDensity);
    // These icons have the same height and width so either is fine for the size.
    final Bitmap phoneIcon = BitmapUtil.drawableToBitmap(phoneDrawable, phoneDrawable.getIntrinsicHeight());

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type if we're pre-O. O created shortcuts have the
    // app badge which overlaps the type overlay.
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);
    if (!BuildCompat.isAtLeastO() && overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    int iconWidth = icon.getWidth();
    if (BuildCompat.isAtLeastO()) {
        // On O we need to calculate where the phone icon goes slightly differently. The whole
        // canvas area is 108dp, a centered circle with a diameter of 66dp is the "safe zone".
        // So we start the drawing the phone icon at
        // 108dp - 21 dp (distance from right edge of safe zone to the edge of the canvas)
        // - 24 dp (size of the phone icon) on the x axis (left)
        // The y axis is simply 21dp for the distance to the safe zone (top).
        // See go/o-icons-eng for more details and a handy picture.
        final int left = (int) (mIconSize - (45 * density));
        final int top = (int) (21 * density);
        canvas.drawBitmap(phoneIcon, left, top, photoPaint);
    } else {
        dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
        canvas.drawBitmap(phoneIcon, null, dst, photoPaint);
    }

    canvas.setBitmap(null);
    return icon;
}

From source file:com.android.leanlauncher.IconCache.java

private Drawable getFullResDefaultIcon(Resources resources, int iconId) {
    Drawable d;//from   w w  w.  j  a  v a2s .com
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

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

From source file:com.android.launcher3.Utilities.java

/**
 * Returns a bitmap suitable for the all apps view. If the package or the resource do not
 * exist, it returns null./*from   w  w w .ja va 2 s  .c o m*/
 */
public static Bitmap createIconBitmap(String packageName, String resourceName, Context context) {
    PackageManager packageManager = context.getPackageManager();
    // the resource
    try {
        Resources resources = packageManager.getResourcesForApplication(packageName);
        if (resources != null) {
            final int id = resources.getIdentifier(resourceName, null, null);
            return createIconBitmap(
                    resources.getDrawableForDensity(id,
                            LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi),
                    context);
        }
    } catch (Exception e) {
        // Icon not found.
    }
    return null;
}