GCM推送通知大图标尺寸

17

您好,我正在使用GCM在Android上实现推送通知。我尝试为通知设置一张图片,而不是默认的应用程序图标。我可以使用以下代码实现这一点:

if(extras.getString("src") != null){
            URL url = new URL(extras.getString("src"));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap large_icon = BitmapFactory.decodeStream(input);
            mBuilder.setLargeIcon(large_icon);
        }

通常情况下,图片将来自于网络(jpg、png等),而不是设备中的图片。上述代码可以工作,但图片可能太大或太小。我想知道位图的最佳尺寸或长宽比,以便我可以提供适当的图片。

3个回答

43

我曾遇到同样的问题,以下是我的解决方法:

首先,您需要知道通知图标的最大尺寸取决于设备的分辨率。经过搜索,我找到了以下信息:

  • ldpi:48x48像素 *0.75
  • mdpi:64x64像素 *1.00
  • hdpi:96x96像素 *1.50
  • xhdpi:128x128像素 *2.00
  • xxhdpi:192x192像素 *3.00

有两种方法可以解决这个问题:

  • 一种是在服务器上拥有一组这些尺寸的图像,并根据您的设备分辨率获取它们。
  • 另一种是在服务器上拥有更大的图像,然后在应用程序中根据您的设备分辨率调整大小。

接下来,我将向您解释我实施的第二种方法。

首先,为了从URL获取图像,我使用了以下代码:

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

然后我需要知道新图像尺寸的因素。我知道在服务器上我有一个因子为*3.00的xxhdpi图像,我使用它来获取全局因子:

public static float getImageFactor(Resources r){
      DisplayMetrics metrics = r.getDisplayMetrics();
      float multiplier=metrics.density/3f;
      return multiplier;
  }

现在我需要调整图像大小,并将新的位图设置为通知图标:

Bitmap bmURL=getBitmapFromURL(largeIcon);
float multiplier= getImageFactor(getResources());
bmURL=Bitmap.createScaledBitmap(bmURL, (int)(bmURL.getWidth()*multiplier), (int)(bmURL.getHeight()*multiplier), false);
if(bmURL!=null){
    mBuilder.setLargeIcon(bmURL);
}           

这个对我很有效,希望你也能用得上。


2
@user25,您所提到的是状态栏图标指南,但OP和回答者讨论的是通过NotificationCompat.Builder.setLargeIcon()设置的图像大小。 - Sufian

11

如果我完全理解了你的问题,下面的内容将对你有所帮助。

如果你已经拥有该图像.. 那么你可以进行如下设置

  .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_app_sky))
  .setSmallIcon(R.drawable.ic_aaja_icon_red)

总数为:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setNumber(COUNTER)
     .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_app_sky))
     .setSmallIcon(R.drawable.ic_icon_red)
     .setAutoCancel(true)
     .setContentTitle(pushCount > 1 ? "xxx" + pushCount : title)
     .setContentText(pushCount > 1 ? "yyy" : message)
     .setWhen(when)
     .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
   //.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(Intent.ACTION_CLEAR_NOTIFICATION), PendingIntent.FLAG_CANCEL_CURRENT))
     .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
     .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

你也可以从这个教程获得帮助。

编辑:要更改位图大小..取自这里..

Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(bitmap , 64, 64, false));

图像将是像这样的URL:http://1.bp.blogspot.com/_rKG-ziTSNUQ/S7H2oaUkKeI/AAAAAAAABTI/DI-jUR-wD1c/s320/blogger-logo.png。我有正方形的图片,只需要知道跨设备的最佳尺寸。 - Ram G Athreya
@RamGAthreya 你可以更改位图大小并将其放入通知中,不需要近似值,应为32像素/64像素。 - Ranjit

0
另外需要知道的是,基本布局在这些图像上有规定的边距,因此如果您想要在自定义布局中模仿基本布局所看到的行为,请确保做类似的事情。请查看notification_template_icon_group.xml以获取详细信息。
在这里,我已经为您计算了像素的数学值(64dp - 12dp):
ldpi 48 - 9 = 39
mdpi 64 - 12 = 52
hdpi 96 - 18 = 78
xhdpi 128 - 24 = 104
xxhdpi 192 - 36 = 156

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接