2

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:

Image scaled correctly

On Android 4.1.x (SDK lvl 16) though, the image is not scaled correctly:

Image NOT scaled correctly

Resolution in both examples is identical.

  • Note that this problem only occurs on a ImageView within a custom notification (big content), not Activities 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 the ImageView 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

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • if you want to keep Image Aspect Ratio then you should use ScaleType centerInside . or you want to fit image then FIT_XY. – Chetan Joshi Feb 21 '17 at 12:54
  • @Chetan centerCrop DOES maintain aspect ration (https://developer.android.com/reference/android/widget/ImageView.ScaleType.html) and should fit the image so one axis matches exactly. – fweigl Feb 21 '17 at 12:56
  • Yes , but it Crop form all side image and show image from center. – Chetan Joshi Feb 21 '17 at 12:57
  • 1
    @Chetan centerCrop not only crops but also scales the image. – fweigl Feb 21 '17 at 13:06

2 Answers2

0

Just set your ImageView's height to wrap_content and add android:adjustViewBounds="true" line to it as well, and it will work:

<?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="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

</LinearLayout>

How it looks on Android 4.1.1, API 16:

enter image description here

romtsn
  • 11,704
  • 2
  • 31
  • 49
  • But was this the initial requirement of @Ascorbin? I think what he tried to achieve is exact same behavior like on Lollipop. – azizbekian Feb 26 '17 at 13:19
  • Firstly, this behavior is not dependent on OS version only, but on screen size as well. Secondly, it's exactly the same behavior as on Lollipop – romtsn Feb 26 '17 at 15:54
  • @rom4ek Unfortunately this only works when the notification view is large enough to hold the image. As soon as the notification reaches it's maximum height, the image is not displayed correctly. I added a TextView above the ImageView to showcase this: http://de.tinypic.com/r/orlesy/9 – fweigl Mar 02 '17 at 11:55
0

From Android API document,

setCustomBigContentView

NotificationCompat.Builder setCustomBigContentView (RemoteViews contentView) Supply custom RemoteViews to use instead of the platform template in the expanded form. This will override the expanded layout that would otherwise be constructed by this Builder object. No-op on versions prior to JELLY_BEAN.

Please check it out below link:

https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setCustomBigContentView(android.widget.RemoteViews)

Updated:

It is not a solution. But it is simple suggestion to achieve that.

private void showNotificationWithImage(Bitmap image) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), `R.layout.notification);`
remoteViews.setImageViewBitmap(R.id.image, image);

Notification notification;
RemoteViews bigView = new RemoteViews(context.getPackageName(),
        R.layout.notification);

bigView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context);
notification = mNotifyBuilder.setContentTitle("Title")
        .setContentText("Slide down to expand")
        .setSmallIcon(R.drawable.n_icon)
        .setLargeIcon(image)
        .setCustomBigContentView(remoteViews)
        .build();
// now show notification..
NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(1, notification);
}

Please check this method. when you swipe down the notification you image will show completely. It is tested in Android_4.2.2.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
  • Jelly Bean is Android 4.1.x (https://source.android.com/source/build-numbers.html), my screenshot was taken on that. This should not be the problem. Edited question for clarity. – fweigl Mar 02 '17 at 13:45
  • Scaling issue remains exactly the same for me with this code :( – fweigl Mar 03 '17 at 08:25