简体   繁体   English

通知大图标看起来很小

[英]Notification large icon looking small

I'm trying to set a drawable in a notification's large icon, but a strange behaviour is happening, the drawable gets smaller for some reason, it's the first notification in this image: 我正在尝试在通知的大图标中设置一个drawable,但是发生了一个奇怪的行为,drawable由于某种原因变小了,这是该图像中的第一个通知:

在此输入图像描述

This is the drawable: 这是可绘制的:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/subject7"/>
        <corners android:radius="30dp"/>
        <padding
            android:bottom="7dp"
            android:left="7dp"
            android:top="7dp"
            android:right="7dp"/>
    </shape>
</item>
<item android:drawable="@drawable/calendar_clock_colored_clicked" />

This is how I'm setting the largeIcon: 这就是我设置largeIcon的方式:

notification = new NotificationCompat.Builder(context);
notification.setLargeIcon(drawableToBitmap(ResourcesCompat.getDrawable(context.getResources(), drawableID, null)));

...

public Bitmap drawableToBitmap (Drawable drawable) {

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

Has this happened to anyone? 这发生在任何人身上吗?

Edit: This is in a BroadcastReceiver, calling itself every 60 seconds to refresh the notification, the first time the largeIcon appears properly, it only appears smaller after the first time. 编辑:这是在BroadcastReceiver中,每60秒调用一次刷新通知,第一次正确显示largeIcon时,它只在第一次显示后变小。

In case someone stumbles into this. 如果有人绊倒了这个。

Make the bitmap with an actual PNG, with the proper padding into it, like this: 使用实际的PNG制作位图,并使用适当的填充,如下所示:

private Bitmap getBitmap(int color) {
    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.notification_class_icon, null);
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);

    //set round background on lolipop+ and square before
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(ContextCompat.getColor(context, color));
        canvas.drawCircle(canvas.getHeight()/2, canvas.getHeight()/2, canvas.getHeight()/2, p);
    }
    else {
        canvas.drawColor(ContextCompat.getColor(context, color));
    }
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);

    return bitmap;
}

This is R.drawable.notification_class_icon looks: 这是R.drawable.notification_class_icon看起来:

在此输入图像描述 (the black is suposed to be transparent). (黑色是透明的)。

Make sure you make one for every dimension. 确保为每个维度制作一个。

Then just set the notifications' icon like this and pass the color for the background: 然后只需设置这样的通知图标并传递背景颜色:

notification.setLargeIcon(getBitmap(R.color.colorPrimary));

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

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