【发布时间】: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