繁体   English   中英

通知图标在Android中显得很小

[英]Notification icon appearing small in Android

看到这张图片 我正在尝试向通知中添加图标,但是通知图标很小,并且无法覆盖整个空间。

我在每个资源文件夹中都有正确的图像尺寸。

我要通知的代码:

Intent myIntent = new Intent(getApplicationContext(), Lock.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(), 
                0, 
                myIntent, 
                Intent.FLAG_ACTIVITY_NEW_TASK);

         myNotification = new NotificationCompat.Builder(getApplicationContext())
           .setContentTitle("Invisible Screen Lock")
           .setContentText("Lock Your Screen")
           .setContentIntent(pendingIntent)
           .setDefaults(Notification.DEFAULT_SOUND)
           .setSmallIcon(R.drawable.launcher)
           .setOngoing(true)
           .build();

         notificationManager = 
           (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
         notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

如何使图标正确显示?

使用setLargeIcon(icon)如下:

myNotification = new NotificationCompat.Builder(getApplicationContext())
       .setContentTitle("Invisible Screen Lock")
       .setContentText("Lock Your Screen")
       .setContentIntent(pendingIntent)
       .setDefaults(Notification.DEFAULT_SOUND)
       .setLargeIcon(icon)
       .setOngoing(true)
       .build();

尝试给出一个大图标

    Notification.Builder nb = new Notification.Builder(context)
    .setContentTitle("title")
    .setContentText("content")
    .setAutoCancel(true)
    .setLargeIcon(drawableToBitmap(YOUR_DRAWABLE))
    .setSmallIcon(R.drawable.small_icon)
    .setTicker(s.getText());
   NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
   nm.notify(100, nb.build());

添加此方法

public static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM