【问题标题】:Resizing image in bigpicture style notification在大图片样式通知中调整图像大小
【发布时间】:2015-06-23 05:54:04
【问题描述】:
我想在我的通知中使用构建器样式的大图片(而不是使用自定义视图)显示图像。图像总是被裁剪。如何根据屏幕比例为不同设备显示正确的图像?
我已经搜索过,我知道我们需要通过根据屏幕宽度缩放图像来显示通知,但我无法实现这一点,并且对 ppi、dpi 和为图像提供的所有其他格式感到困惑。请任何人提供代码来转换位图以根据屏幕尺寸在不同设备上以适当的格式显示?
【问题讨论】:
标签:
android
image
bitmap
push-notification
【解决方案1】:
这是您缩放接收到的位图的方式,其中第二个和第三个参数分别是宽度和高度
Bitmap b = Bitmap.createScaledBitmap(receivedBitmap, 120, 120, false);
如果您想根据宽度保持纵横比进行缩放,请执行此操作
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;
}
要生成带有缩放位图的通知,请使用此
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();