I'm trying to fit (center and inside) 2 kind of images obtained from web services in a Big Picture styled Notification. One kind is 97px*150px sized and the other 300px*100px.
As I checked the image must fit 256dp tall max height to fit on Jelly Bean's Big Picture notification visual area, so I expected that I can call the matrix scales availables on ImageView (fitXT, centerInside, etc) but i'm surprised that no scaling method behavior is available to configure and the bitmap is always center-cropped wich by the way is worst default matrix behavior I would selected for default because images always are cutted in some way. A center inside behavior for default it would be better, I think.
So, this leaves me room for this posibilities:
- Pre-scale images using some sort of pixels to dp method. This method must also has to respect aspect ratio. After that use that new picture against Notification Builder, wich (I expect) will respect the iomages because no out of bounds behavior will happen.
- Create my own RemoteView to use ImageView and gain access to scale matrix behaviors.
- File a new feature request to Google :)
- File a bug request to Google :)
Which option do you think or know as the best?
Is there any other option that am I could be missing?
EDIT: I've tested several methods to matrix scale the images with success but the center-crop is always there by default to break and cut my images making it imposible to use.
I will give a try now to RemoteView method.
I also filed the issue 58318 for this, trying to make life easier for future devs trying to accomplish this task: https://code.google.com/p/android/issues/detail?id=58318
The only solution is to create my own RemoteView as follows:
First, a custom layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_bar_latest_event_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background">
<ImageView
android:id="@+id/big_icon"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:scaleType="fitCenter"
/>
<TextView
android:id="@+id/title"
android:text="sometitlestringfromresources"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignBottom="@+id/big_icon"
android:layout_toRightOf="@+id/big_icon"
android:layout_marginLeft="15dp"/>
<ImageView
android:id="@+id/big_picture"
android:layout_width="match_parent"
android:layout_height="192dp"
android:layout_marginTop="64dp"
android:scaleType="fitCenter" //This scale will show your picture without crop
/>
And the RemoteView implementation:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
RemoteViews views;
views = new RemoteViews(getPackageName(), R.layout.custom_notification);
views.setImageViewBitmap(R.id.big_picture, bitmap);
views.setImageViewBitmap(R.id.big_icon, BitmapFactory.decodeResource(getResources(), R.drawable.your_24x24dp_brand_icon));
views.setTextViewText(R.id.title, message);
myNotification.bigContentView = views;
}
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(randomInt, myNotification);
Where myNotification is your notification builder and randomInt is my own implementation to no overlap notifications (check if this is not your need to replace notifications or generate several).
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
And that's it. Maybe is a little dirty but it works. I'll leave the bug open to Google and if I receive some response i'll update this.