2016-03-07 40 views
1

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

在未擴展版本中,它看起來應該如此。圓圈,擬合圖標。 enter image description here

但是在擴展版本中,它看起來好像是從縮放版本的圖標中刪除。 enter image description here

對於圖標我以下:

 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

回答

0

最後我找到了......好吧,我不會把它叫做一個解決方案......解決方法。看來,在LayerDrawable的圖層上設置插圖會導致圖像的大小增加。有些設備似乎裁剪圖像,而不是適合它們,如果它們太大。

這使我在將其設置爲LargeIcon之前調整了iconBitmap的大小。

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。或者,也許這只是一些黑魔法...

如果有人想出了一個更好的解決方案,我會接受一個。