[英]Notification large icon looking small
我正在嘗試在通知的大圖標中設置一個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時,它只在第一次顯示后變小。
如果有人絆倒了這個。
使用實際的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看起來:
確保為每個維度制作一個。
然后只需設置這樣的通知圖標並傳遞背景顏色:
notification.setLargeIcon(getBitmap(R.color.colorPrimary));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.