我正在尝试在通知的大图标中设置一个drawable,但是发生了奇怪的行为,drawable由于某种原因变小了,这是此图像中的第一个通知:
这是可绘制的:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/subject7"/>
<corners android:radius="30dp"/>
<padding
android:bottom="7dp"
android:left="7dp"
android:top="7dp"
android:right="7dp"/>
</shape>
</item>
<item android:drawable="@drawable/calendar_clock_colored_clicked" />
这就是我设置largeIcon的方式:
notification = new NotificationCompat.Builder(context);
notification.setLargeIcon(drawableToBitmap(ResourcesCompat.getDrawable(context.getResources(), drawableID, null)));
...
public 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, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
这是否发生在任何人身上?
编辑:这是在BroadcastReceiver中,每60秒调用一次刷新通知,第一次正确显示largeIcon时,它只在第一次显示后变小。
答案 0 :(得分:0)
万一有人绊倒了。
使用实际的PNG制作位图,并使用适当的填充,如下所示:
private Bitmap getBitmap(int color) {
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.notification_class_icon, null);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
//set round background on lolipop+ and square before
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(ContextCompat.getColor(context, color));
canvas.drawCircle(canvas.getHeight()/2, canvas.getHeight()/2, canvas.getHeight()/2, p);
}
else {
canvas.drawColor(ContextCompat.getColor(context, color));
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
这是R.drawable.notification_class_icon看起来:
确保为每个维度制作一个。
然后只需设置通知&#39;像这样的图标并传递背景颜色:
notification.setLargeIcon(getBitmap(R.color.colorPrimary));