gpt4 book ai didi

android - 使用 Drawable 创建一个通知栏

转载 作者:行者123 更新时间:2023-11-29 21:48:27 26 4
gpt4 key购买 nike

我正在尝试使用 Drawable 对象创建自定义通知栏,但它在参数中没有接收到 Drawable,只有 Bitmap.

问题:

我需要获取另一个应用程序的图标以在我的通知中设置。我尝试使用下面的代码:

getPackageManager().getApplicationIcon(packageName);//Return a Drawable Object

//Problem
Notification.Builder builder = new Notification.Builder(context);
builder.setLargeIcon(Bitmap); //I don´t have this one, only a Drawable object.

如您所见。我如何使用此对象放入我的通知栏?

预期结果:

使用另一个应用程序的图标、名称和对这个应用程序的操作创建一个通知栏。

解决方案:

谢谢,pietmau 和 kadrei。

请使用下面的代码:

public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

并完成这个:

Notification.Builder builder = new Notification.Builder(context);

Drawable icon = getPackageManager().getApplicationIcon(
packageName);
Bitmap bitmapIcon = drawableToBitmap(icon);

builder.setIcon(android.R.drawable.stat_sys_download_done).//It´s necessary put a resource here. If you don´t put any resource, then the Notification Bar is not show.
setLargeIcon(bitmapIcon);

Notification notification = builder.getNotification();
notificationManager.notify(yourId, notification);

最佳答案

也许这有帮助:

public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

来源: https://stackoverflow.com/a/9390776/1955332

关于android - 使用 Drawable 创建一个通知栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com