I create a custom (big) notification with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
and create the notification like this:
private void showNotificationWithImage(Bitmap image) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification);
remoteViews.setImageViewBitmap(R.id.image, image);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.n_icon)
.setCustomBigContentView(remoteViews)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0))
.build();
notificationManager.notify(0, notification);
}
The image displayed is this one.
On Android 5.0 upwards, the image is scaled correctly (centerCrop
, y-axis fits exactly) within the ImageView
:
On Android 4.1.x (SDK lvl 16) though, the image is not scaled correctly:
Resolution in both examples is identical.
Note that this problem only occurs on a
ImageView
within a custom notification (big content), notActivities
etc.Using
fitCenter
etc. is not an option because the image should span the whole width (OR height)I tried setting the ImageView height to
wrap_content
, same problem. It seems to work when setting theImageView
height so it rougly fits the images aspect ration, but that is not an option either as I want to display images of different aspect ratios.
Anyone know how to fix this?
PS: You can check out my test project here.
EDIT: Setting the image's height to wrap_content
and adding adjustViewBounds="true"
as suggested by rom4ek results in the following as soon as the notification hits it's maximum heigth: Link to Image