【问题标题】:How to convert a Icon to Bitmap in android? [duplicate]如何在android中将图标转换为位图? [复制]
【发布时间】:2016-09-12 03:16:55
【问题描述】:

我想在 android 中将 Icon 转换为 Bitmap 对象。

我的目标是将通知图标转换为位图。

我该怎么办??

谢谢大家的回答!

【问题讨论】:

  • 谢谢大家。我用反射方法(loadDrawableInner)解决了。
  • 我不认为这个问题是完全重复的,虽然相似。不同之处在于Icon 不是Drawable,但您可以使用IconIcon 获得Drawableicon.loadDrawable(context)
  • 使用这个代码:Icon icon = notification.getSmallIcon();位图 bitmap = icon.loadDrawable(context);

标签: android bitmap icons


【解决方案1】:

希望对你有帮助

Bitmap bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

【讨论】:

  • 感谢您的回答。但是,我想将通知对象中的图标转换为位图。
【解决方案2】:

你可以使用这个类。我也经常使用这个代码。

public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;

if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    if(bitmapDrawable.getBitmap() != null) {
        return bitmapDrawable.getBitmap();
    }
}

if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
    bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;

}

【讨论】:

  • 感谢您的回答。但是, Notification.getSmallIcon() 返回的不是 Drawable 而是 Icon 对象...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 2018-07-21
  • 2011-08-05
  • 2019-11-16
  • 2011-06-07
  • 2016-03-25
相关资源
最近更新 更多