在bigpicture样式通知中调整图像大小

时间:2021-12-01 09:01:09

I want to display an image in my notification using builder style big picture (and not using the custom view). The image is coming cropped always. How should I display the proper image for different devices depending on the screen ratios?

我想使用构建器样式大图在我的通知中显示图像(而不是使用自定义视图)。图像即将被裁剪。如何根据屏幕比例显示不同设备的正确图像?

I have searched and I have got that we need to display the notification by scaling the image depending on the screen width but I am not able to implement that and getting too confused with ppi, dpi and all other formats provided for image. Please can anyone provide me with the code to convert the bitmap to be shown in proper format on the different devices depending on the screen size?

我已经搜索过,我已经得到了我们需要通过根据屏幕宽度缩放图像来显示通知但我无法实现它并且与ppi,dpi和为图像提供的所有其他格式混淆。请任何人都可以根据屏幕大小向我提供转换位图的代码,以便在不同的设备上以适当的格式显示?

1 个解决方案

#1


0  

This is how you scale your received bitmap where 2nd and 3rd arguments are width and height respectively

这是扩展接收位图的方法,其中第二个和第三个参数分别是宽度和高度

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

Incase you want to scale by maintaining the aspect ratio according to your width, then 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 the notification with scaled bitmap , use this

并使用缩放位图生成通知,使用此

 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();

#1


0  

This is how you scale your received bitmap where 2nd and 3rd arguments are width and height respectively

这是扩展接收位图的方法,其中第二个和第三个参数分别是宽度和高度

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

Incase you want to scale by maintaining the aspect ratio according to your width, then 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 the notification with scaled bitmap , use this

并使用缩放位图生成通知,使用此

 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();