【问题标题】:GCM Push Notification Large Icon sizeGCM 推送通知大图标大小
【发布时间】:2014-09-21 17:37:09
【问题描述】:

您好,我正在使用 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 等),而不是设备中的任何东西。上面的代码有效,但图像太大或太小。我想知道位图的最佳尺寸或纵横比,以便提供合适的图像

【问题讨论】:

    标签: android android-notifications google-cloud-messaging


    【解决方案1】:

    我遇到了同样的问题。我就是这样解决的:

    首先,您需要了解通知图标的最大尺寸,具体取决于设备分辨率。搜索,我发现了这个:

    • 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】:

    如果我完全理解您的问题,那么以下内容将对您有所帮助。

    如果你已经有图片了..那么你可以设置它

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

    您也可以从这个tutorial..获得帮助。

    编辑:更改位图大小..取自here..

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

    【讨论】:

    【解决方案3】:

    这里要知道的另一件事是基本布局在这些图像上声明了边距,因此如果您试图模仿自定义布局中基本布局所看到的行为,请确保执行类似的操作。查看 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多