4

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

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

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

  • 使用某种像素到 dp 方法对图像进行预缩放。这种方法还必须尊重纵横比。之后使用该新图片对抗通知生成器,它(我希望)将尊重 iimages,因为不会发生越界行为。
  • 创建我自己的 RemoteView 以使用 ImageView 并获得对比例矩阵行为的访问权限。
  • 向 Google 提交新功能请求 :)
  • 向 Google 提交错误请求 :)

您认为或知道哪个选项最好?我还有其他选择吗?

编辑:我已经测试了几种方法来成功地对图像进行矩阵缩放,但是默认情况下,中心裁剪总是在那里破坏和剪切我的图像,使其无法使用。

我现在试试 RemoteView 方法。

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

4

1 回答 1

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

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

于 2014-10-28T21:31:18.413 回答