1

我想在一个大通知图标中放置 4 个位图。每个位图应放置在图标的四分之一处。

我为图标创建了一个布局“notification_large_icon_layout.xml”:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_large_icon_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/thumbnail1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_contact_picture" />

    <ImageView
        android:id="@+id/thumbnail2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/thumbnail1"
        android:src="@drawable/ic_contact_picture" />

    <ImageView
        android:id="@+id/thumbnail3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/thumbnail1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_contact_picture" />

    <ImageView
        android:id="@+id/thumbnail4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/thumbnail2"
        android:layout_alignRight="@+id/thumbnail3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_contact_picture" />

</RelativeLayout>

我在通知函数中添加了以下代码:

...
// Create bitmap and scale it to 1/2
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(rowUri));
Resources res = getApplicationContext().getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
bitmap = Bitmap.createScaledBitmap(bitmap, width/2, height/2, false);

// Inflate the layout for the large icon                            
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.notification_large_icon_layout, null);

// Copy bitmap to the 4 views
ImageView thumbnailView = (ImageView) view.findViewById(R.id.thumbnail1);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail3);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail4);
thumbnailView.setImageBitmap(bitmap);

// Convert view to bitmap
Bitmap icon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
view.draw(canvas);

notificationBuilder.setLargeIcon(icon);
...

它不起作用。图标只有灰色。

变化一:

我已将布局更改如下(不太灵活):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_large_icon_layout"
    android:layout_width="128dp"
    android:layout_height="128dp" >

    <ImageView
        android:id="@+id/thumbnail1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <ImageView
        android:id="@+id/thumbnail2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/thumbnail1" />

    <ImageView
        android:id="@+id/thumbnail3"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/thumbnail1" />

    <ImageView
        android:id="@+id/thumbnail4"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/thumbnail2"
        android:layout_toRightOf="@+id/thumbnail3" />

</RelativeLayout>

我已经改变了这样的功能:

...
// Create bitmap and scale it to 1/2
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(rowUri));
Resources res = getApplicationContext().getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
bitmap = Bitmap.createScaledBitmap(bitmap, width/2, height/2, false);

// Inflate the layout for the large icon                            
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.notification_large_icon_layout, null);

view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

// Copy bitmap to the 4 views
ImageView thumbnailView = (ImageView) view.findViewById(R.id.thumbnail1);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail3);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail4);
thumbnailView.setImageBitmap(bitmap);

// Convert view to bitmap
Bitmap icon = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
view.draw(canvas);

notificationBuilder.setLargeIcon(icon);
...

现在我可以看到 1 个具有一半分辨率/密度的位图。似乎 4 个位图重叠。

变化2:

以下代码有效:

...
// Create bitmap and scale it to 1/2
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(rowUri));
Resources res = getApplicationContext().getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
bitmap = Bitmap.createScaledBitmap(bitmap, width/2, height/2, false);

// Inflate the layout for the large icon
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.notification_large_icon_layout, null);
view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

// Copy bitmap to the 4 views
ImageView thumbnailView = (ImageView) view.findViewById(R.id.thumbnail1);
thumbnailView.setBottom(height/2);
thumbnailView.setRight(width/2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail2);
thumbnailView.setBottom(height/2);
thumbnailView.setLeft(width/2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail3);
thumbnailView.setTop(height/2);
thumbnailView.setRight(width/2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail4);
thumbnailView.setTop(height/2);
thumbnailView.setLeft(width/2);
thumbnailView.setImageBitmap(bitmap);

// Convert view to bitmap
Bitmap icon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
view.draw(canvas);

notificationBuilder.setLargeIcon(icon);
...

但我想知道,为什么我必须明确定义位图的位置。

顺便说一句:以下片段 A 和 B 具有相同的结果。我看不出有什么区别。

片段一:

Bitmap icon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
view.draw(canvas);

片段 B:

view.setDrawingCacheEnabled(true);
Bitmap icon = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
4

1 回答 1

0

我发现了我的错误。ImageView 的 android:src 属性引用的图标大小错误。将大小更改为 64 x 64 后,布局可以正确膨胀:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notification_large_icon_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/thumbnail1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_icon" />

<ImageView
    android:id="@+id/thumbnail2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/thumbnail1"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_icon" />

<ImageView
    android:id="@+id/thumbnail3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/thumbnail1"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_icon" />

<ImageView
    android:id="@+id/thumbnail4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/thumbnail2"
    android:layout_toRightOf="@+id/thumbnail3"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_icon" />

</RelativeLayout>

我的来源现在看起来像这样:

...
Resources res = getApplicationContext().getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);

// Inflate the layout for the large icon
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.notification_large_icon_layout, null);
view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

// Copy bitmap to the 4 views                           
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(rowUri));
ImageView thumbnailView = (ImageView) view.findViewById(R.id.thumbnail1);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail2);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail3);
thumbnailView.setImageBitmap(bitmap);
thumbnailView = (ImageView) view.findViewById(R.id.thumbnail4);
thumbnailView.setImageBitmap(bitmap);

// Convert view to bitmap
Bitmap icon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
view.draw(canvas);

notificationBuilder.setLargeIcon(icon);
...
于 2013-08-20T20:55:20.790 回答