我想在一个大的通知图标中放置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);
...
它不起作用。图标只是灰色。
更改1:
我改变了布局如下(不太灵活):
<?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);
...
现在我可以看到一个半分辨率/密度的位图。似乎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);
...
但我想知道,为什么我必须明确定义位图的位置。
Btw:以下片段A和B具有相同的结果。我看不出有任何区别。
代码段A:
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);
答案 0 :(得分: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);
...