Java Code Examples for android.graphics.drawable.BitmapDrawable#setTargetDensity()

The following examples show how to use android.graphics.drawable.BitmapDrawable#setTargetDensity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuggestionStripLayoutHelper.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: SuggestionStripLayoutHelper.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: ACache.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: Utils.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: ACache.java    From gank.io-unofficial-android-client with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: MemoryCache.java    From MemorySpinner with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: CacheUtil.java    From MicroReader with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: XulBitmapUtil.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: ACache.java    From DMusic with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: DevCacheUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: ApplicationPackageManager.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 12
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 13
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 15
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 19
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
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;
}