【问题标题】:ScaleType cropCenter not working on custom Notification layout - Android 4.xScaleType cropCenter 不适用于自定义通知布局 - Android 4.x
【发布时间】:2017-02-21 12:42:02
【问题描述】:

我创建了一个具有以下布局的自定义(大)通知:

<?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>

并像这样创建通知:

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);
}

显示的图片是this one

在 Android 5.0 上,图像正确缩放centerCrop,y 轴完全适合)ImageView

在 Android 4.1.x(SDK lvl 16)上,图像未正确缩放

两个示例中的分辨率相同。

  • 请注意,此问题仅发生在自定义通知(大内容)中的ImageView,而不是Activities 等。

  • 使用fitCenter 等不是一个选项,因为图像应该跨越整个宽度(或高度)

  • 我尝试将 ImageView 高度设置为wrap_content,同样的问题。设置ImageView 高度时它似乎有效,因此它大致适合图像纵横比,但这也不是一个选项,因为我想显示不同纵横比的图像。

有人知道如何解决这个问题吗?

PS:你可以查看我的测试项目here

编辑:将图像的高度设置为wrap_content 并按照rom4ek 的建议添加adjustViewBounds="true",一旦通知达到其最大高度,就会出现以下结果:Link to Image

【问题讨论】:

  • 如果你想保持图像纵横比,那么你应该使用 ScaleType centerInside 。或者你想适合图像然后 FIT_XY。
  • @Chetan centerCrop 确实保持纵横比 (developer.android.com/reference/android/widget/…) 并且应该适合图像,因此一个轴完全匹配。
  • 是的,但是它会裁剪所有侧面图像并从中心显示图像。
  • @Chetan centerCrop 不仅可以裁剪,还可以缩放图像。

标签: android android-imageview android-notifications android-image


【解决方案1】:

只需将ImageView 的高度设置为wrap_content 并添加android:adjustViewBounds="true" 行,它就可以工作了:

<?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>

它在 Android 4.1.1、API 16 上的外观:

【讨论】:

  • 但这是@Ascorbin 的初始要求吗?我认为他试图实现的行为与 Lollipop 完全相同。
  • 首先,此行为不仅取决于操作系统版本,还取决于屏幕大小。其次,它与 Lollipop 上的行为完全相同
  • @rom4ek 不幸的是,这仅在通知视图足够大以容纳图像时才有效。一旦通知达到其最大高度,图像将无法正确显示。我在 ImageView 上方添加了一个 TextView 来展示这一点:de.tinypic.com/r/orlesy/9
【解决方案2】:

来自 Android API 文档,

setCustomBigContentView

NotificationCompat.Builder setCustomBigContentView (RemoteViews contentView) 提供自定义 RemoteViews 以代替扩展表单中的平台模板使用。这将覆盖由此 Builder 对象构建的扩展布局。 JELLY_BEAN 之前的版本无操作。

请查看以下链接:

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

更新:

这不是一个解决方案。但这是实现这一目标的简单建议。

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);
}

请检查此方法。当您向下滑动通知时,您的图像将完全显示。在Android_4.2.2中测试。

【讨论】:

  • Jelly Bean 是 Android 4.1.x (source.android.com/source/build-numbers.html),我的截图就是在上面拍摄的。这应该不是问题。为了清楚起见,编辑了问题。
  • 使用此代码对我来说,缩放问题仍然完全相同:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多