如何在安卓系统中让一张图片适应自定义的256dp大图通知?

4
我正在尝试将两种从Web服务获取的图片(一种是97px * 150px大小的,另一种是300px * 100px)置于Big Picture样式通知中心且居中显示。我发现在Jelly Bean的Big Picture通知视觉区域中,图像的最大高度必须适应256dp,因此我期望可以调用ImageView中可用的矩阵缩放比例(fitXT、centerInside等),但我惊讶地发现没有可配置的缩放方法行为,位图始终是居中裁剪的,这是最糟糕的默认矩阵行为,因为图像总是以某种方式被切断。我认为,默认情况下采用居中内部行为会更好。

因此,这留给我以下几种可能性:

  • 使用某种像素到dp的预缩放图像方法。该方法还必须保持宽高比。之后,使用新图片构建通知,(我期望)不会出现越界行为,因此可以正常显示图片。
  • 创建自己的RemoteView来使用ImageView并获得访问缩放矩阵行为的权限。
  • 向Google提出新的功能请求 :)
  • 向Google提交错误报告 :)

你认为哪个选项是最好的?还有其他我可能会错过的选项吗?

编辑:我已经成功测试了几种矩阵缩放图像的方法,但是默认情况下始终存在居中裁剪,这使得我的图片无法使用。

现在我将尝试RemoteView方法。

我还为此提交了问题58318,试图为未来试图完成此任务的开发人员简化生活:https://code.google.com/p/android/issues/detail?id=58318

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

就是这样。可能有点不太规范,但它能用。我将把这个漏洞留给Google,如果我收到一些回应,我会更新此内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接