为了避免Android MediaStyle通知图片出现马赛克,建议使用高清晰度的图片作为largeIcon,并调整其尺寸以适应通知尺寸。以下是示例代码:
Kotlin:
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification_icon)
val size = resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height)
val resizedBitmap = Bitmap.createScaledBitmap(bitmap, size, size, false)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Title")
.setContentText("Content text")
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(resizedBitmap)
.setStyle(NotificationCompat.MediaStyle())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
Java:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon);
int size = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, size, size, false);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Title")
.setContentText("Content text")
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(resizedBitmap)
.setStyle(new NotificationCompat.MediaStyle())
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
其中,上述代码中的"notification_icon"为要显示的图片资源文件名。通过调整resizedBitmap的大小,从而解决Android MediaStyle 通知图片出现马赛克的问题。