蜂窝通知 - 如何将largeIcon设置为正确的大小?

发布于 2024-12-02 07:21:43 字数 556 浏览 7 评论 0原文

我发现自己很好奇为什么Notification.Builder 上的setLargeIcon 方法只接受Bitmap,没有重载来提供资源id。也许这是出于性能原因,但这似乎很奇怪,因为 setSmallIcon 确实接受 res drawable id。

Notification.Builder builder = new Notification.Builder(application);
// ....
builder.setLargeIcon(iconBitmap);  // Requires a Bitmap
builder.setSmallIcon(iconResId);   // Requires a drawable resource ID
Notification notification = builder.getNotification();

遗憾的是,提供的位图在通知中未缩放,因此需要为通知视图提供完全正确的尺寸。

假设我需要提供 LargeIcon 位图的 xhdpi、hdpi、mdpi 和 ldpi 版本,它们需要多大尺寸?我在文档中或在更广泛的网络上搜索后看不到任何提及。

I find myself curious why the setLargeIcon method on Notification.Builder only accepts a Bitmap, with no overload to provide a resource id. Perhaps it was done for performance reasons, but it seems odd as setSmallIcon does accept a res drawable id.

Notification.Builder builder = new Notification.Builder(application);
// ....
builder.setLargeIcon(iconBitmap);  // Requires a Bitmap
builder.setSmallIcon(iconResId);   // Requires a drawable resource ID
Notification notification = builder.getNotification();

Sadly the bitmap provided is not scaled in the notification, so the Bitmap needs to be provided exactly the right size for the notification view.

Assuming I need to provide xhdpi, hdpi, mdpi and ldpi versions of the largeIcon bitmap, what sizes do they need to be? I can see no mention in the docs, or after scouring the wider web.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2024-12-09 07:21:43

还没有机会检查它,但 API 11 引入了以下公共尺寸:

应该可以在将其设置在通知上之前使用它们来缩放图像。

Not had a chance to check it yet but API 11 introduced the following public dimens:

Should be able to use those to scale your image before setting it on the notification.

墨落成白 2024-12-09 07:21:43

我使用 通知大图标的尺寸 创建缩放位图

BitmapDrawable contactPicDrawable = (BitmapDrawable) ContactsUtils.getContactPic(mContext, contactId);
Bitmap contactPic = contactPicDrawable.getBitmap();

Resources res = mContext.getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
contactPic = Bitmap.createScaledBitmap(contactPic, width, height, false); 

然后我设置带有此缩放位图的大图标。

I used the dimensions of the notification's large icon to create a scaled bitmap

BitmapDrawable contactPicDrawable = (BitmapDrawable) ContactsUtils.getContactPic(mContext, contactId);
Bitmap contactPic = contactPicDrawable.getBitmap();

Resources res = mContext.getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
contactPic = Bitmap.createScaledBitmap(contactPic, width, height, false); 

And then I set the large icon with this scaled bitamp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文