Resize image in bigpicture style notification

I want to display the image in my notification using a large builder style drawing (and not using a custom view). The image is always close. How to display the correct image for different devices depending on the aspect ratio of the screen?

I searched, and it turned out that we need to display a notification, scaling the image depending on the width of the screen, but I can not realize this and get too confused with ppi, dpi and all other formats provided for the image. Can someone provide me a code to convert a bitmap that will be displayed in the proper format on different devices depending on the screen size?

+6
source share
1 answer

Here's how you scale the resulting bitmap, where the 2nd and 3rd arguments are equal to the width and height, respectively

Bitmap b = Bitmap.createScaledBitmap(receivedBitmap, 120, 120, false); 

If you want to scale while maintaining the aspect ratio to fit your width, do this

 public Bitmap getResizedBitmap(Bitmap bm, int width ) { float aspectRatio = bm.getWidth() / (float) bm.getHeight(); height = Math.round(width / aspectRatio); Bitmap resizedBitmap = Bitmap.createScaledBitmap( bm, width, height, false); return resizedBitmap; } 

and to generate a notification with a scaled bitmap use

  NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(mContext) .setContentTitle("set title") .setContentText("Swipe Down to View") .setSmallIcon(mContext.getApplicationInfo().icon) .setLargeIcon(receivedBitmap) .setContentIntent(pendingIntent) .setDefaults(Notification.DEFAULT_SOUND) .setStyle(new Notification.BigPictureStyle() .bigPicture(b)) .setPriority(Notification.PRIORITY_HIGH) .setVibrate(new long[0]) .build(); 
0
source

Source: https://habr.com/ru/post/989575/


All Articles