我有一个支持私有版和公共版的通知。私有版本显示为bigTextStyle。两个版本的图标都生成为LayerDrawable,然后转换为位图。这适用于所有设备。除了一个在地狱中伪造的华为Ascent Mate 7之外。 (Android 4.4.2,EMUI 3.0)
对于我执行的图标:
Drawable background = ContextCompat.getDrawable(this, R.drawable.shape_notification_circle);
if (background != null) {
PorterDuffColorFilter filter = new PorterDuffColorFilter(ThemeManager.getInstance().getTheme()
.getColorMainDark(), PorterDuff.Mode.SRC_ATOP);
background.setColorFilter(filter);
}
Drawable[] layers = {background, ContextCompat.getDrawable(this, icon)};
//icon is an int, containing the resource id
LayerDrawable layerDrawable = new LayerDrawable(layers);
int padding = dpToPx(24);
layerDrawable.setLayerInset(1, padding, padding, padding, padding);
Bitmap iconBitmap = drawableToBitmap(layerDrawable);
方法drawableToBitmap:
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap;
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;
}
shape_notification_circle的布局
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#666666"/>
<size
android:width="48dp"
android:height="48dp"/>
</shape>
图标drawable:
<?xml version="1.0" encoding="UTF-8"?>
<vector android:height="48dp"
android:viewportHeight="1000.0"
android:viewportWidth="1000.0"
android:width="48dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="#FFFFFF"
android:pathData="M500,609.8l-75.20001,-72.79999l-299.69998,252.79999l749.80005,0l-299.7,-252.79999z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M122.6,210.2l377.4,365l377.40002,-365z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M406.3,519.7l-300.9,-292.2l0,546.3z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M894.6,773.8l0,-546.3l-300.89996,292.2z"/>
</vector>
最后,创建通知。 (notificationBuilder和notificationBuilderPublic曾经不同,但现在是相同的,除了不同的bigTextStyle)
// @formatter:off
NotificationCompat.Builder notificationBuilderPublic = new NotificationCompat.Builder(this)
.setLargeIcon(iconBitmap)
.setSmallIcon(R.drawable.ic_notification_launcher)
.setColor(ThemeManager.getInstance().getTheme().getColorAccent())
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(getString(R.string.app_name))
.setContentText(title)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setCategory(category)
.setPriority(priority)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(iconBitmap)
.setSmallIcon(R.drawable.ic_notification_launcher)
.setColor(ThemeManager.getInstance().getTheme().getColorAccent())
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(getString(R.string.app_name))
.setContentText(title)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setCategory(category)
.setPriority(priority)
.setVisibility(visibility)
;
// @formatter:on
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)
.setBigContentTitle(getString(R.string.app_name)).setSummaryText(extraSummary));
notificationBuilder.setPublicVersion(notificationBuilderPublic.build());
notificationManager.notify(pushId + "|" + ownermoduleid + "|" + fid, type.getId(), notificationBuilder.build());
我做错了什么?
编辑:添加了布局和图标XML
答案 0 :(得分:0)
最后我发现......好吧,我不会称它为解决方案......一种解决方法。看来,在LayerDrawable的图层上设置插图将导致图像的大小增加。有些设备似乎裁剪图像而不是适合它们,如果它们太大的话。
这导致我在将iconBitmap设置为LargeIcon之前调整其大小。
Bitmap iconBitmap = scaleDown(drawableToBitmap(layerDrawable), dpToPx(48), true);
缩放功能
public Bitmap scaleDown(Bitmap source, int size, boolean filter) {
return Bitmap.createScaledBitmap(source, size, size, filter);
}
这仍然无法完全完美无缺。扩展通知时,图像稍微小一点,但这对我来说没问题。仍然比裁剪好。
我仍然不知道,为什么只有在扩展视图时才会发生这种情况。也许华为的人员实现了与普通风格不同的BigTextStyle。或许它只是一些黑魔法......
如果有人想出更好的解决方案,我会接受那个。