我有一個支持私有和公共版本的通知。私人版本顯示爲bigTextStyle。兩個版本的圖標生成爲LayerDrawable,然後轉換爲位圖。這適用於所有設備。除了在地獄中僞造的華爲Ascent Mate 7之外。 (Android 4.4.2,EMUI 3.0)NotificationCompat中的BigTextStyle導致圖形錯誤
對於圖標我以下:
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>
的圖標繪製:
<?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