我正在尝试使用带有位图图像的 Notification.Builder.setLargeIcon(bitmap) . 我有我想在可绘制文件夹中使用的图像,那么如何将其转换为位图?
Notification.Builder.setLargeIcon(bitmap)
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context 可以是您当前的 Activity .
Context
Activity
首先创建位图图像
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
现在在Notification Builder Icon中设置位图....
Notification.Builder.setLargeIcon(bmp);
在 res/drawable 文件夹中,
res/drawable
1. 创建一个新的 Drawable Resources .
Drawable Resources
2. 输入文件名 .
将在 res/drawable 文件夹中创建一个新文件 .
在新创建的文件中替换此代码,并将 ic_action_back 替换为您的可绘制文件名 .
ic_action_back
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/ic_action_back" android:tint="@color/color_primary_text" />
现在,您可以将它与资源ID一起使用, R.id.filename .
R.id.filename
Drawable myDrawable = getResources().getDrawable(R.drawable.logo); Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
由于API 22 getResources().getDrawable() 已弃用,因此我们可以使用以下解决方案 .
getResources().getDrawable()
Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo, getContext().getTheme()); Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
你可能意味着 Notification.Builder.setLargeIcon(Bitmap) ,对吗? :)
Notification.Builder.setLargeIcon(Bitmap)
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon); notBuilder.setLargeIcon(largeIcon);
这是将资源图像转换为Android Bitmap 的好方法 .
Bitmap
这是另一种将Drawable资源转换为android中的Bitmap的方法:
Drawable drawable = getResources().getDrawable(R.drawable.input); Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
6 回答
Context
可以是您当前的Activity
.首先创建位图图像
现在在Notification Builder Icon中设置位图....
在
res/drawable
文件夹中,1. 创建一个新的
Drawable Resources
.2. 输入文件名 .
将在
res/drawable
文件夹中创建一个新文件 .在新创建的文件中替换此代码,并将
ic_action_back
替换为您的可绘制文件名 .现在,您可以将它与资源ID一起使用,
R.id.filename
.由于API 22
getResources().getDrawable()
已弃用,因此我们可以使用以下解决方案 .你可能意味着
Notification.Builder.setLargeIcon(Bitmap)
,对吗? :)这是将资源图像转换为Android
Bitmap
的好方法 .这是另一种将Drawable资源转换为android中的Bitmap的方法: