如何在Android中使图像适合自定义256dp大图片通知?
我试图将从web服务获得的两种图像(中间和内部)放入一个大图片样式的通知中。一种尺寸为97px*150px,另一种尺寸为300px*100px 当我检查时,图像必须符合256dp的最大高度,才能符合Jelly Bean的大图片通知可视区域,因此我希望可以调用ImageView上可用的矩阵比例(fitXT、centerInside等)但我很惊讶,没有缩放方法行为可供配置,位图总是居中裁剪,顺便说一句,这是我默认选择的最糟糕的默认矩阵行为,因为图像总是以某种方式剪切。我认为,一个默认的中心内部行为会更好 因此,这给我留下了一些空间:如何在Android中使图像适合自定义256dp大图片通知?,android,scale,android-imageview,android-notifications,Android,Scale,Android Imageview,Android Notifications,我试图将从web服务获得的两种图像(中间和内部)放入一个大图片样式的通知中。一种尺寸为97px*150px,另一种尺寸为300px*100px 当我检查时,图像必须符合256dp的最大高度,才能符合Jelly Bean的大图片通知可视区域,因此我希望可以调用ImageView上可用的矩阵比例(fitXT、centerInside等)但我很惊讶,没有缩放方法行为可供配置,位图总是居中裁剪,顺便说一句,这是我默认选择的最糟糕的默认矩阵行为,因为图像总是以某种方式剪切。我认为,一个默认的中心内部行为会
- 使用某种像素到dp方法预缩放图像。该方法还必须考虑纵横比。在对Notification Builder使用新图片之后,wich(我希望)会尊重iomages,因为不会发生越界行为
- 创建我自己的RemoteView以使用ImageView并访问缩放矩阵行为
- 向Google提交新功能请求:)
- 向谷歌提交bug请求:)
我也为此提交了58318号文件,试图让未来的开发人员更轻松地完成这项任务:唯一的解决方案是创建我自己的RemoteView,如下所示: 首先,自定义布局:
<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
/>
其中myNotification是您的通知生成器,randomInt是我自己的无重叠通知实现(请检查您是否需要替换通知或生成多个通知)
就这样。也许有点脏,但它是有效的。我会把这个bug留给谷歌,如果我收到一些回复,我会更新这个
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);
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);