setTargetDensity()源码实例Demo

android.graphics.drawable.BitmapDrawable#setTargetDensity()源码实例Demo

下面列出了android.graphics.drawable.BitmapDrawable#setTargetDensity() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openboard   文件: SuggestionStripLayoutHelper.java
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize,
        final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}
 
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize,
        final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}
 
源代码3 项目: DevUtils   文件: DevCacheUtils.java
/**
 * Bitmap 转 Drawable
 * @param bitmap {@link Bitmap}
 * @return {@link Drawable}
 */
protected static Drawable bitmapToDrawable(final Bitmap bitmap) {
    if (bitmap == null) return null;
    try {
        BitmapDrawable drawable = new BitmapDrawable(bitmap);
        drawable.setTargetDensity(bitmap.getDensity());
        return new BitmapDrawable(bitmap);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "bitmapToDrawable");
    }
    return null;
}
 
源代码4 项目: DMusic   文件: ACache.java
@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}
 
源代码5 项目: starcor.xul   文件: XulBitmapUtil.java
/**
 * Bitmap 转 Drawable
 */
@SuppressWarnings("deprecation")
public static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}
 
源代码6 项目: MicroReader   文件: CacheUtil.java
@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}
 
源代码7 项目: MemorySpinner   文件: MemoryCache.java
@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
	if (bm == null) {
		return null;
	}
	BitmapDrawable bd=new BitmapDrawable(bm);
	bd.setTargetDensity(bm.getDensity());
	return new BitmapDrawable(bm);
}
 
@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
  if (bm == null) {
    return null;
  }
  BitmapDrawable bd = new BitmapDrawable(bm);
  bd.setTargetDensity(bm.getDensity());
  return new BitmapDrawable(bm);
}
 
源代码9 项目: AndroidBase   文件: Utils.java
@SuppressWarnings("deprecation")
public static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    BitmapDrawable bd = new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}
 
源代码10 项目: fingerpoetry-android   文件: ACache.java
@SuppressWarnings("deprecation")
private static Drawable bitmap2Drawable(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    BitmapDrawable bd=new BitmapDrawable(bm);
    bd.setTargetDensity(bm.getDensity());
    return new BitmapDrawable(bm);
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}