如何使图像适合Android中的自定义256dp大图片通知?

时间:2013-07-28 14:20:18

标签: android scale android-imageview android-notifications

我正试图在大画面风格的通知中适合(中心和内部)从Web服务获得的2种图像。一种是97px * 150px大小,另一种是300px * 100px。

当我检查时,图像必须符合256dp高的最大高度以适应Jelly Bean的大图通知视觉区域,所以我希望我可以调用ImageView上的矩阵比例(fitXT,centerInside等),但我很惊讶没有可用于配置的缩放方法行为,并且位图始终是中心裁剪的,这是我默认选择的最差默认矩阵行为,因为图像总是以某种方式被剪切。我认为,默认情况下,中心内部行为会更好。

所以,这给我留下了这个可能性的空间:

  • 使用某种像素对dp方法预缩放图像。此方法还必须遵守纵横比。之后使用针对Notification Builder的新图片,我(我希望)会尊重iomages,因为不会发生越界行为。
  • 创建我自己的RemoteView以使用ImageView并获得对缩放矩阵行为的访问权。
  • 向Google提交新的功能请求:)
  • 向Google提交错误请求:)

您认为或最了解哪个选项? 我还有其他选择吗?

编辑:我已经测试了几种方法来成功地对图像进行矩阵缩放,但默认情况下,中心裁剪会断开并剪切我的图像,使其无法使用。

我现在尝试使用RemoteView方法。

我也为此提交了问题5831​​8,试图让未来的开发人员更轻松地完成这项任务:https://code.google.com/p/android/issues/detail?id=58318

1 个答案:

答案 0 :(得分:1)

唯一的解决方案是按如下方式创建我自己的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
        />

RemoteView实现:

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

myNotification是你的通知构建器,randomInt是我自己的实现,没有重叠通知(检查是否需要替换通知或生成多个通知)。

  Random randomGenerator = new Random();
  int randomInt = randomGenerator.nextInt(100);

就是这样。也许有点脏,但它的确有效。我会把这个bug留给谷歌,如果我收到一些回复,我会更新。